2.3. Virtual Environment#
We want to create a project directory for your project and a Python virtual environment within that directory for managing Python and all installed package files.
2.3.1. Creating Project Directory#
In general, when we create a project, it means we are creating a folder/directory. To use a project directory, we use the CLI of your OS. Since they are CLI rather than GUI, we need to use commands to achieve this. Although this can be done with a file manager (GUI), knowing how to use the CLI is a good skill set to have in the long run. The three shell commands that we use here are: ls (“list storage”), cd (“change directory”), and mkdir (“make directory”).
For the purpose of this course, we want to create a workspace directory inside your user home directory, and a dsm directory inside the workspace directory.
Note that macOS and Linux use forward slash for directory separator, while Windows use backward slash as separator but can also interpret forward slashes.
We use PowerShell as our CLI here because the syntax is more friendly.
PS C:\Users\[user]> mkdir workspace
Directory: C:\Users\[user]
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 8/31/2025 3:14 AM workspace
PS C:\Users\[user]> cd workspace
PS C:\Users\[user]\workspace> mkdir dsm
Directory: C:\Users\[user]\workspace
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 8/31/2025 3:14 AM dsm
PS C:\Users\[user]\workspace> cd dsm
PS C:\Users\[user]\workspace\dsm>
Use Terminal.app as CLI here for this task.
[user]@[host]ː~$ mkdir workspace
[user]@[host]ː~$ cd workspace
[user]@[host]ː~/workspace$ mkdir dsm
[user]@[host]ː~/workspace$ cd dsm/
[user]@[host]ː~/workspace/dsm$
After issuing the above commands, you will have a project directory (dsm) inside the workspace directory, and your current location is inside the dsm project directory.
2.3.2. Installing Virtual Environment#
In the dsm project directory, we will create a Python virtual environment for the project. This means that we will place all installed packages (dependencies) that we use in the site-packages directory of the virtual environment rather than installing them in the system Python, which is prone to conflicts when different versions of software packages are installed or upgraded.
On the other hand, installing the packages in the project virtual environment also means that we are making this environment portable, meaning we can replicate the dependencies in another project or device. Note that, in addition to the installed project, the virtual environment also contains a link to the desired Python version.
To create the virtual environment for the project, we issue the command as python -m venv .venv, in which python is the default desired version of Python, -m means module, venv is the module that creates virtual environment, and .venv, as a convention, is the name of the project virtual environment.
The syntax for creating a Python virtual environment is:
python -m venv <environment_path_and_name>
Since we want to control which version of Python to use (3.12 in this case), we will use the version suffix by issuing py -3.12 -m venv .venv (Windows) or python3.12 -m venv .venv (macOS) to specify the Python version to use (which may be different from the system default Python version):
We use PowerShell here because the syntax is more friendly.
PS C:\Users\[user]\workspace\dsm> py -3.12 -m venv .venv
PS C:\Users\[user]\workspace\dsm> ls
Directory: C:\Users\[user]\workspace\dsm
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 8/31/2025 3:36 AM .venv
PS C:\Users\[user]\workspace\dsm> ls
Directory: C:\Users\[user]\workspace\dsm
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 8/31/2025 3:36 AM .venv
Use Terminal.app for this task.
[user]@[host]ː~/workspace/dsm$ python3.12 -m venv .venv
[user]@[host]ː~/workspace/dsm$ ls -a
. .. .venv
[user]@[host]ː~/workspace/dsm$
Using the ls command, we see that the virtual environment .venv folder has been created.
Make sure you are using PowerShell as your CLI here. If you are using Command Prompt, issue dir instead of ls to list files.
PS C:\Users\[user]\workspace\dsm> ls
Directory: C:\Users\[user]\workspace\dsm
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 9/4/2025 9:41 PM .ipynb_checkpoints
d----- 9/4/2025 9:41 PM .venv
In macOS, using ls you will not see the .venv folder because file and directory names beginning with a dot are hidden. To see the .venv virtual environment in macOS, add the -a option to the ls command: $ ls -a.
[user]@[host]ː~/workspace/dsm$ ls -a
. .venv labs test.ipynb
.. assignments_grading NumPy test.py
.DS_Store data numpy_arrays.ipynb
Inside the .venv virtual environment directory, there is a lib/Lib directory containing a folder called site-packages, where all packages are installed. Additionally, all binary codes, including links to Python executables, the venv activation scripts, pip, and other program executables, are installed in the bin (macOS) or Scripts (Windows) folder.
In Windows, the site-packages directory appears as follows. So far, we only have pip, but every package you install will have a folder here, and soon the list will be long.
PS C:\Users\[user]\workspace\dsm> ls .venv\lib\site-packages\
Directory: C:\Users\[user]\workspace\dsm\.venv\Lib\site-packages
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 8/31/2025 3:36 AM pip
d----- 8/31/2025 3:36 AM pip-24.0.dist-info
In macOS, the site-packages folder looks like below. So far we only have pip but every package you install will come here and soon the list will be long.
[user]@[host]ː~/workspace/dsm$ ls .venv/lib/python3.12/site-packages/
pip pip-24.0.dist-info
2.3.3. Activating/Deactivating venv#
To activate your virtual environment, in the CLI, make sure you are in the project directory (dsm) and type the following command to activate the project virtual environment. After the virtual environment is activated, a prefix (“(.venv)”) will be present in front of the CLI prompt.
[user]@[host]ː~/workspace/dsm$ source .venv/bin/activate
(.venv) [user]@[host]ː~/workspace/dsm$
PS C:\Users\[user]\workspace\dsm> .\.venv\Scripts\activate
(.venv) PS C:\Users\[user]\workspace\dsm>
When the virtual environment is successfully activated, you will see (.venv) appears before the shell’s prompt line, which tells us the virtual environment is activated, we are using the designated version of Python, and that all the packages installation will go into the site-packages directory.
Note that, after you are done working on the project, you need to deactivate the virtual environment to prevent the installation of dependencies intended for other projects into this virtual environment. To do that, use the deactivate command (you can deactivate a virtual environment anywhere; it does not have to be in the project folder):
(.venv) PS C:\Users\[user]\workspace\dsm> deactivate
PS C:\Users\[user]\workspace\dsm>
(.venv) [user]@[host]:~/workspace/dsm$ deactivate
[user]@[host]:~/workspace/dsm$