Best practices to use and manage Python virtual environments
Virtual environments are a useful tool for managing the packages and libraries that you use for different projects.
Here are the main points to follow when using virtual environments:
Create a new virtual environment for each project, using a tool like
virtualenv
orvenv
. This will allow you to have a separate environment for each project, with its own installed packages and libraries.Use the
-p
flag to specify the version of Python that you want to use for your virtual environment. This will ensure that you are using the correct version of Python for your project, and that any packages you install are compatible with that version.Use a requirements file to specify the packages that your project depends on. This makes it easy to install all the necessary packages into a new virtual environment, and ensures that all members of your team are working with the same package versions.
By following these best practices, you can effectively use virtual environments to manage the packages and libraries for your Python projects.
Creating a virtual environment using venv
To create a new virtual environment using venv
, you can use the following steps:
Open a terminal or command prompt.
Choose a directory where you want to create your virtual environment.
Run the following command to create a new virtual environment called
myenv
:
python -m venv myenv
This will create a new directory called myenv
that contains the files and directories needed for the virtual environment.
To activate the virtual environment, use the following command:
source myenv/bin/activate
You should now see the name of your virtual environment in the terminal prompt, indicating that it is active. For example:
(myenv) $
To deactivate the virtual environment, use the following command:
deactivate
You can now use the virtual environment to install packages and manage your project dependencies.
Setting the Python version to use in your virtual environment
To specify the version of Python that you want to use for your virtual environment, you can use the -p
flag when creating the environment with venv
. For example, to create a virtual environment called myenv
that uses Python 3.8, you can use the following command:
python3.8 -m venv myenv
You can also specify the full path to the Python executable if it is not in your PATH
. For example:
/usr/local/bin/python3.8 -m venv myenv
Keep in mind that you need to have the specific version of Python installed on your system to create a virtual environment using that version.
Once the virtual environment is created, you can use the python
executable inside the virtual environment to run Python scripts and manage packages. This will ensure that you are using the correct version of Python for your project.
Using a requirements file to specify the packages your project needs
A requirements file is a text file that specifies the packages that your Python project depends on. It is a useful way to manage the dependencies for your project, and makes it easy to install all the necessary packages into a new virtual environment.
To create a requirements file for your project, you can use the pip freeze
command. This command generates a list of all the packages and their versions that are currently installed in your virtual environment.
For example, suppose you have a virtual environment with the following packages installed:
Flask==1.1.2
requests==2.24.0
To create a requirements file, open a terminal or command prompt and activate your virtual environment. Then run the following command:
pip freeze > requirements.txt
This will create a file called requirements.txt
in your project directory, with the following contents:
Flask==1.1.2
requests==2.24.0
The requirements file specifies the packages and their exact versions that your project depends on. You can then use this file to install all the necessary packages into a new virtual environment by running the following command:
pip install -r requirements.txt
This will install all the packages listed in the requirements file into the new virtual environment.
By using a requirements file, you can ensure that all members of your team are working with the same package versions and that you can easily set up a new development environment for your project.
Hope it helps you create virtual environments and manage the package that your project needs. :)
Ref: https://docs.python.org/3.9/library/venv.html
Ref: https://pip.pypa.io/en/stable/cli/pip_freeze/
Subscribe to my newsletter
Read articles from Paulo Rodrigues directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Paulo Rodrigues
Paulo Rodrigues
Hey there! My name is Paulo and I am an experienced Data Solutions Architect. I have a software dev background {php, js, python, go} and am currently workings as a Solutions Architect in a Brazilian company involved in Big Data, Machine Learning and AI.