Setting up a Python Virtual Environment (venv)
A concise guide on how to set up a Python virtual environment (venv) with CLI commands, rendered in Markdown, and a CSV cheat sheet:
Setting up a Python Virtual Environment (venv)
-
Open a terminal or command prompt.
-
Navigate to the directory where you want to create your virtual environment.
cd /path/to/your/project/directory
-
Create a new virtual environment using the
python -m venv
command.python -m venv my_venv
This will create a new directory called
my_venv
in your current directory, which will contain the Python interpreter and all the packages you install. -
Activate the virtual environment.
-
On Windows:
my_venv\Scripts\activate
-
On macOS or Linux:
source my_venv/bin/activate
You should see
(my_venv)
at the beginning of your command prompt, indicating that the virtual environment is active. -
-
Install packages in the virtual environment.
pip install <package_name>
For example, to install the
numpy
package:pip install numpy
-
Deactivate the virtual environment when you're done.
deactivate
This will return you to your system's default Python environment.
Virtual Environment Cheat Sheet (CSV)
| Command | Description |
| --- | --- |
| python -m venv my_venv
| Create a new virtual environment named my_venv
|
| source my_venv/bin/activate
| Activate the virtual environment on macOS or Linux |
| my_venv\Scripts\activate
| Activate the virtual environment on Windows |
| deactivate
| Deactivate the virtual environment |
| pip install <package_name>
| Install a package in the virtual environment |
| pip freeze > requirements.txt
| Save the installed packages to a requirements.txt
file |
| pip install -r requirements.txt
| Install packages from a requirements.txt
file |
Remember, always activate your virtual environment before installing or using packages to ensure that your project's dependencies are isolated from the system's default Python environment.