To Beginners Learning with Minimum Python Environment Setup
tags: Python
, Virtual Environment
Since there are lots of editors (e.g. Visual Studio Code (VSCode), Sublime, Atom, Notepad++), IDEs (Integrated Development Environment) (e.g. PyCharm, Visual Studio), web-based applications (e.g. Colab) support people to work with Python, but that always make those who don’t familiar with basic computer or programming knowledge really confusing.
What is the better setup for leaning Python ? Of course Colab is one of the simplest environment working with Python, just open a notebook. But what if working on your own PC or laptop ?
Download the Python execution from: https://www.python.org/downloads/ , then follow the instructions to complete the installation.
[Recommended]
- Windows Installer (64-bit)
- macOS default comes with Python (version 3) execution already
- Linux can be downloaded with apt
or yum
package manager
example:
## Linux (with sudo privilege)
$ sudo apt install python3 python3-venv
For Windows installation wizard, please check
the option Add python.exe to PATH
at the beginning, and Disable path length limit
at the end of wizard.

: "Add python.exe to PATH"

Disable path length limit
”Next, check the execution works fine along with:
Beware that Windows points
python
to indicate Python version 3
Linux separatespython
as Python version 2,python3
as Python version 3
## [Windows] Command Prompt (CMD) / PowerShell (PS)
C:\Users\user> python --version
Python 3.11.3
## macOS / Linux
user@localhost $ python3 --version
Python 3.11.3
Now we are going to setup a project / working directory (aka. folder) on the Desktop, take folder name hello_world
for example:
## [Windows] Command Prompt (CMD) / Powershell (PS)
C:\Users\user> cd Desktop
C:\Users\user\Desktop> mkdir hello_world
## macOS / Linux
user@localhost:~ $ cd Desktop
user@localhost:~/Desktop $ mkdir hello_world
For different purposes & dependencies for each project, we need venv
(aka. Virtual Environment) to separate & isolate the packages.
P.S. $ python3 -m venv venv
means calling for Python execution with the -m
flag which using the module venv
then make the virtual environment name as venv
as the last parameter from the above command, choose your own venv name if you want.
## [Windows] Command Prompt (CMD) / PowerShell (PS)
C:\Users\user\Desktop> cd hello_world
C:\Users\user\Desktop\hello_world> python -m venv venv
## macOS / Linux
user@localhost:~/Desktop $ cd hello_world
user@localhost:~/Desktop/hello_world $ python3 -m venv venv
Then there will have a new folder called venv
inside the working directory hello_world
, next we are going to activate the virtual environment.
## [Windows] Command Prompt (CMD)
C:\Users\user\Desktop\hello_world> .\venv\Scripts\activate.bat
## [Windows] PowerShell (PS)
PS C:\Users\user\Desktop\hello_world> .\venv\Scripts\Activate.ps1
## macOS / Linux /* NOTICE: Check & set the exec permission */
user@localhost:~/Desktop/hello_world $ sudo chmod +x ./venv/bin/activate
user@localhost:~/Desktop/hello_world $ source ./venv/bin/activate
PowerShell must have ExecutionPolicy
set before activate ps1 file.
ref: https://superuser.com/a/106362
Once we activate venv successfully, the console of the terminal will have the prefix indicated that we are inside the venv:
## [Windows] Command Prompt (CMD)
(venv) C:\Users\user\Desktop\hello_world>
## [Windows] PowerShell (PS)
(venv) PS C:\Users\user\Desktop\hello_world>
## macOS / Linux
(venv) user@localhost:~/Desktop/hello_world $
### To deactivate ###
## [Windows] Command Prompt (CMD) / PowerShell (PS)
(venv) > deactivate
>
## macOS / Linux
(venv) $ deactivate
$
Then how to check we are using the correct Python execution ?
## [Windows] PowerShell (PS)
(venv) PS C:\Users\user\Desktop\hello_world> Get-Command python
(venv) PS C:\Users\user\Desktop\hello_world> gcm python
CommandType Name Version Source
----------- ---- ------- ------
Application python.exe 3.11.31... C:\Users\user\Desktop\hello_world\venv\Scripts\python.exe
(venv) PS C:\Users\user\Desktop\hello_world> Get-Command pip
(venv) PS C:\Users\user\Desktop\hello_world> gcm pip
CommandType Name Version Source
----------- ---- ------- ------
Application pip.exe 0.0.0.0 C:\Users\user\Desktop\hello_world\venv\Scripts\pip.exe
## macOS / Linux
(venv) $ which python3
/home/user/Desktop/hello_world/venv/bin/python3
(venv) $ which pip3
/home/user/Desktop/hello_world/venv/bin/pip3
Then create a main.py
for testing, with Linux you can try $ touch main.py
for create or short command for create + edit by vim: $ vim main.py
Install vim in Linux by :
$ sudo apt install vim -y
$ sudo yum install vim -y
# filename: main.py
print("hello world")
## [Windows] Command Prompt (CMD) / PowerShell (PS)
(venv) > python main.py
## macOS / Linux
(venv) $ python3 main.py
### Output ###
hello world
Mostly we want to add dependencies for the project, just with the command $ pip install
.
If there are lots of dependencies, comes with the purpose to management about package and versions, create a requirements.txt
to store more information about them, and remember to install them inside the virtual environment, example:
## filename: requirememts.txt
pandas
numpy>=1.24.3
## [Windows] Command Prompt (CMD) / PowerShell (PS)
(venv) > pip install -r requirements.txt
## macOS / Linux
(venv) $ pip3 install -r requirements.txt
We had just setup the basic for the Python working directory !
Now it’s time to add your own codes and idea to make it happened.
For the editor I recommend Visual Studio Code
(link) made by Microsoft, and the IDE goes to PyCharm
(link) from JetBrains.
VSCode works great for most of the cases, and has been nicely supported for basic Python script with extensions, so does ipynb
format as IPython Notebook. Just select the correct interpreter then run the script, that’s all.
Keep enjoy coding with Python! Hope this article helps.