Python >> Tips and Tricks 6
Table of Contents
This tutorial will introduce how to create virtual environment in Python.
venv
To create a virtual Python environment, you can use the venv
module that is included in the Python standard library. Here’s how you can create a virtual environment and activate it:
Open a terminal window and navigate to the directory where you want to create your virtual environment.
Run the following command to create a virtual environment:
python3 -m venv myenv
This will create a new directory called myenv that contains the files and directories needed for your virtual environment.
To activate the virtual environment, run the following command:
source myenv/bin/activate
This will update your terminal prompt to indicate that you are now working within the virtual environment. You should see the name of your virtual environment in the prompt, like this:
(myenv) user@machine:~/path/to/project$
To deactivate the virtual environment, simply run the following command:
deactivate
This will exit the virtual environment and return you to the global Python environment.
virtualenv
You can also use the virtualenv package to create and manage virtual environments. To install virtualenv, run the following command:
pip install virtualenv
Once virtualenv is installed, you can use it to create a virtual environment like this:
virtualenv myenv
This will create a new virtual environment in the myenv directory. To activate the virtual environment, run the following command:
source myenv/bin/activate
To deactivate the virtual environment, run the following command:
deactivate
Virtual environments are useful for isolating the packages and dependencies of a specific project, so that you can have different packages and versions of packages installed for different projects without them conflicting with each other.
pipenv
pipenv is another tool that you can use to manage virtual environments and install Python packages. It combines pip (the Python package manager) and virtualenv into a single command-line tool, and provides additional features such as automatic package management and easy creation of virtual environments.
To install pipenv, you can use the pip package manager by running the following command:
pip install pipenv
Once pipenv is installed, you can use it to create a new virtual environment and install packages by running the following command:
pipenv install package_name
This will create a new virtual environment and install the package package_name in it. You can also specify a specific version of the package by appending ==version to the package name, like this:
pipenv install package_name==1.0.0
To activate the virtual environment created by pipenv, run the following command:
pipenv shell
This will open a new shell session within the virtual environment. You can also run Python commands within the virtual environment by using the pipenv run
command, like this:
pipenv run python my_script.py
To deactivate the virtual environment, simply exit the shell session by running the exit command or pressing CTRL+D
.
You can also use pipenv
to manage the packages in your virtual environment. For example, you can use the pipenv install
command to install new packages, the pipenv update command to update existing packages, and the pipenv uninstall
command to uninstall packages.
pipenv install new_package
pipenv update existing_package
pipenv uninstall package_to_remove
pipenv is a convenient tool for managing virtual environments and packages, as it simplifies the process of creating and maintaining separate environments for different projects. It is also helpful for reproducing a specific package setup, as it creates a Pipfile
and Pipfile.lock
in the project directory that specify the exact packages and versions that are installed in the virtual environment.