QUICK TIP - How to install / connect / freeze a Python Virtual Environment

- 1. QUICK TIP - Most Useful .htaccess Tricks for WordPress
- 2. QUICK TIP - How to Join and Earn From Brave Ads
It is often useful to have one or more Python environments where you can experiment with different combinations of packages without affecting your main installation. Python supports this through virtual environments. The virtual environment is a copy of an existing version of Python with the option to inherit existing packages. A virtual environment is also useful when you need to work on a shared system and do not have permission to install packages as you will be able to install them in the virtual environment.
Outline
- Open a terminal
- Setup the pip package manager
- Install the virtualenv package
- Create the virtual environment
- Activate the virtual environment
- Deactivate the virtual environment
- Optional: Make the virtual environment your default Python
- More: Python virtualenv documentation
Requirements
- An installation of Python
Jargon
Link to Jargon page with terms: terminal
Open a terminal
The method you used to open a terminal depends on your operating system.
Windows
Open the Windows Command Prompt (show path via Start menu and keyboard shortcuts)
Mac OS / Linux
Open the Terminal program. This is usually found under Utilities or Accessories.
Setup the pip package manager
Check to see if your Python installation has pip. Enter the following in your terminal:
1 |
pip -h |
If you see the help text for pip then you have pip installed, otherwise download and install pip
Install the virtualenv package
The virtualenv package is required to create virtual environments. You can install it with pip:
1 |
pip install virtualenv |
Create the virtual environment
To create a virtual environment, you must specify a path. For example to create one in the local directory called ‘mypython’, type the following:
1 |
virtualenv mypython |
Activate the virtual environment
You can activate the python environment by running the following command:
Mac OS / Linux
1 |
source mypython/bin/activate |
Windows
1 |
mypthon\Scripts\activate |
You should see the name of your virtual environment in brackets on your terminal line e.g. (mypython).
Any python commands you use will now work with your virtual environment
Deactivate the virtual environment
To deactivate the virtual environment and use your original Python environment, simply type ‘deactivate’.
1 |
deactivate |
Installing packages
Now that you’re in your virtual environment you can install packages. Let’s install the Requests library from the Python Package Index (PyPI):
1 |
pip install requests |
pip should download requests and all of its dependencies and install them:
1 2 3 4 5 6 7 8 9 10 11 12 |
Collecting requests Using cached requests-2.18.4-py2.py3-none-any.whl Collecting chardet<3.1.0,>=3.0.2 (from requests) Using cached chardet-3.0.4-py2.py3-none-any.whl Collecting urllib3<1.23,>=1.21.1 (from requests) Using cached urllib3-1.22-py2.py3-none-any.whl Collecting certifi>=2017.4.17 (from requests) Using cached certifi-2017.7.27.1-py2.py3-none-any.whl Collecting idna<2.7,>=2.5 (from requests) Using cached idna-2.6-py2.py3-none-any.whl Installing collected packages: chardet, urllib3, certifi, idna, requests Successfully installed certifi-2017.7.27.1 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22 |
Installing specific versions
pip allows you to specify which version of a package to install using version specifiers. For example, to install a specific version of requests
:
1 |
pip install requests==2.18.4 |
To install the latest 2.x
release of requests:
1 |
pip install requests>=2.0.0,<3.0.0 |
To install pre-release versions of packages, use the --pre
flag:
1 |
pip install --pre requests |
Installing extras
Some packages have optional extras. You can tell pip to install these by specifying the extra in brackets:
1 |
pip install requests[security] |
Installing from source
pip can install a package directly from source, for example:
1 2 |
cd google-auth pip install . |
Additionally, pip can install packages from source in development mode, meaning that changes to the source directory will immediately affect the installed package without needing to re-install:
1 |
pip install --editable . |
Installing from version control systems
pip can install packages directly from their version control system. For example, you can install directly from a git repository:
1 |
git+https://github.com/GoogleCloudPlatform/google-auth-library-python.git#egg=google-auth |
For more information on supported version control systems and syntax, see pip’s documentation on VCS Support.
Installing from local archives
If you have a local copy of a Distribution Package’s archive (a zip, wheel, or tar file) you can install it directly with pip:
1 |
pip install requests-2.18.4.tar.gz |
If you have a directory containing archives of multiple packages, you can tell pip to look for packages there and not to use the Python Package Index (PyPI) at all:
1 |
pip install --no-index --find-links=/local/dir/ requests |
This is useful if you are installing packages on a system with limited connectivity or if you want to strictly control the origin of distribution packages.
Using other package indexes
If you want to download packages from a different index than the Python Package Index (PyPI), you can use the --index-url
flag:
1 |
pip install --index-url http://index.example.com/simple/ SomeProject |
If you want to allow packages from both the Python Package Index (PyPI) and a separate index, you can use the --extra-index-url
flag instead:
1 |
pip install --extra-index-url http://index.example.com/simple/ SomeProject |
Upgrading packages
pip can upgrade packages in-place using the --upgrade
flag. For example, to install the latest version of requests
and all of its dependencies:
1 |
pip install --upgrade requests |
Using requirements files
Instead of installing packages individually, pip allows you to declare all dependencies in a Requirements File. For example you could create a requirements.txt
file containing:
1 2 |
requests==2.18.4 google-auth==1.1.0 |
And tell pip to install all of the packages in this file using the -r
flag:
1 |
pip install -r requirements.txt |
Freezing dependencies
Pip can export a list of all installed packages and their versions using the freeze
command:
1 |
pip freeze |
Which will output a list of package specifiers such as:
1 2 3 4 5 6 7 8 9 10 11 |
cachetools==2.0.1 certifi==2017.7.27.1 chardet==3.0.4 google-auth==1.1.1 idna==2.6 pyasn1==0.3.6 pyasn1-modules==0.1.4 requests==2.18.4 rsa==3.4.2 six==1.11.0 urllib3==1.22 |
This is useful for creating Requirements Files that can re-create the exact versions of all packages installed in an environment.