Python tutorials > Modules and Packages > Packages > How to use virtual environments?
How to use virtual environments?
venv
and discuss best practices.
Why Use Virtual Environments?
Creating a Virtual Environment (venv)
venv
module, part of the Python standard library, is used to create virtual environments.
Explanation:
python3 -m venv
: This command invokes the venv
module.myenv
: This is the name of the virtual environment. You can choose any name you like. This command will create a directory named myenv
in your current directory.
python3 -m venv myenv
Activating the Virtual Environment
Explanation:
After activation, your shell prompt will typically change to indicate the name of the active environment (e.g., source myenv/bin/activate
executes the activation script within the myenv/bin
directory. The source
command ensures that the changes to the environment are applied to your current shell.myenv\Scripts\activate
executes the activation script within the myenv\Scripts
directory.(myenv) $
).
source myenv/bin/activate # On Linux/macOS
myenv\Scripts\activate # On Windows
Installing Packages in the Virtual Environment
pip
. pip
will install the packages into the environment's site-packages directory, isolating them from other environments and your global Python installation.
Explanation:
pip install requests
: Installs the requests
library, a popular package for making HTTP requests.
pip install requests
Deactivating the Virtual Environment
Explanation:
deactivate
: This command removes the virtual environment's settings from your shell, returning it to its normal state. Your shell prompt will return to its original form.
deactivate
Listing Installed Packages
pip freeze
. This command outputs a list of packages with their versions, which is useful for creating a requirements.txt
file.
Explanation:
pip freeze
: Outputs a list of installed packages in the format package_name==version
.
pip freeze
Creating a requirements.txt
File
requirements.txt
file lists all the dependencies of your project, allowing others (or yourself in the future) to easily recreate the environment. This is crucial for project reproducibility.
Explanation:
pip freeze > requirements.txt
: Redirects the output of pip freeze
to a file named requirements.txt
. This file will contain a list of all installed packages and their versions.
pip freeze > requirements.txt
Installing Packages from requirements.txt
requirements.txt
file, use the following command:
Explanation:
pip install -r requirements.txt
: Installs all packages listed in the requirements.txt
file, ensuring that the environment is configured with the correct dependencies. The -r
flag tells pip
to read the requirements from the specified file.
pip install -r requirements.txt
Real-Life Use Case
Best Practices
requirements.txt
file: Always create a requirements.txt
file to document your project's dependencies..venv
, env
, or the project name) and stick to it.myenv/
or .venv/
) to your .gitignore
file to prevent it from being committed to your repository. Virtual environments contain platform-specific binaries and should not be shared directly.
When to Use Virtual Environments
Alternatives
venv
is the standard, other tools exist for managing virtual environments:venv
and provides similar functionality. It supports older versions of Python.pyproject.toml
file to manage dependencies and simplifies the process of creating and managing virtual environments.Pipfile
as you install/uninstall packages.
Pros of Using Virtual Environments
Cons of Using Virtual Environments
FAQ
-
What if I forget to activate the virtual environment?
If you install packages without activating the virtual environment, they will be installed globally. This can lead to conflicts and make your project less portable. Always activate the environment before installing dependencies. -
How do I delete a virtual environment?
To delete a virtual environment, simply delete the directory containing the environment (e.g.,myenv/
). Make sure the environment is deactivated before deleting it. -
Can I have multiple virtual environments active at the same time?
No, you can only have one virtual environment active at a time in a single shell. Activating a new environment will deactivate the previously active one. -
Why is my activated virtual environment not showing the environment name in the prompt?
This can happen if the activation script fails to properly modify the prompt. Check your shell's configuration or try reactivating the environment.