Python Virtual Environments - pip
Setting up virtual python environment - pip
This article is for Linux environments with focus on pip. For conda see this page.
Managing project dependencies can quickly become a headache, especially when working on multiple Python projects simultaneously. That's why I rely heavily on virtual environments. On my Ubuntu system, I use virtual environments to create isolated spaces for each project or specific aspect of a project. This ensures that each environment has its own set of libraries and dependencies, preventing conflicts and allowing for precise version control. For example, one project might require an older version of a library, while another benefits from the latest release.
Check the version by running the following command in your terminal:
python3 -VIn my case, this returns
Python 3.10.6 which will be the base Python used when creating my virtual environments.{In case pip is no installed ( Linux), use user with sudo: apt install pip}
{In case of error: externally-managed-environment
read this and use user with sudo apt install python3-venv}
For installing venv via pip use the following command:
python3 -m pip install --user venvpython3 -m venv venvdemo{python3.11 -m venv venvdemo if you need the venv based on a specific python version installed}.
This command will create a new folder named
venvdemo within your current directory. If you prefer to place the virtual environment in a different location, simply provide the desired path as the second argument:python3 -m venv /path/to/venvdemoInside the venvdemo folder, you'll find several important components:
pyvenv.cfg: This configuration file stores information about the Python version used to create the environment. You can inspect this file to verify the base Python interpreter.bin: This directory contains the activation script (activate)
To activate the virtual environment , you need to run the :
activate script. Use the following commandsource /path/to/venvdemo/bin/activateUpon successful activation, your terminal prompt will change to indicate that the virtual environment is active. You'll see the name of your environment in parentheses, like this:
(venvdemo) 5q@5q-PC:~/somePath/venvDemoFolder$Creating an Alias for Easy Activation
Instead of typing the full path to activate your virtual environment each time, you can create an alias for quick access. If you're currently in the
venvdemo directory, run the following command to add an alias to your ~/.bashrc file:echo "alias venvdemo=\"source $(pwd)/bin/activate\"" >> ~/.bashrcAfter running this, either close and reopen your terminal or run source ~/.bashrc to apply the changes. Now, you can activate your
venvdemo environment simply by typing venvdemo in your terminal.Activate the environment and install some useful libraries, as you would usually do. Here is an example:
python3 -m pip install numpypython3 -m pip install pandasUse the pip freeze command to make a snapshot of the packages and their versions.
python3 -m pip freeze > requirements.txtThis will create a
requirements.txt file in your current directory, containing a list of your installed packages and their versions. You can view this file using a text editor.Open the file to see the list with libraries:
open requirements.txtTo install the same packages in a new environment, activate that environment and run::
python -m pip install -r ../pathToTheFile/requirements.txtTo deactivate the current virtual environment, simply run:
deactivateTo delete the virtual environment entirely, remove its folder recursively:
rm -r venvdemoIf you want to move your environment in different directory, just get the requirements and recreate it in the new place.
How to Use Your Virtual Environment in Jupyter Lab
To ensure your Jupyter Lab notebook uses the correct packages from your virtual environment, follow these steps:
-
Activate your virtual environment.
- Replace venvdemo with the actual name of your virtual environment.
- Install the
ipykernelpackage. This allows Jupyter Lab to recognize your virtual environment's kernel.
- The env might need to be registered in the kernelspec. Inside the activated venv run:
python3 -m ipykernel install --user --name=venvdemo- Launch Jupyter Lab
Select your virtual environment's kernel.
- In Jupyter Lab, open the "Kernel" menu.
- Choose "Change kernel..."
- Select the kernel that corresponds to your virtual environment's name (
your_venv_name).
Verify the kernel is active.
- Create a new cell in your Jupyter Lab notebook.
- Enter the following command into the cell:
- import sys print(sys.executable)
- Run the cell.
- The output should display the path to the Python executable within your virtual environment, confirming that the correct kernel is in use.
