The Python Virtual Environments and Packages | Python 3 Packages Tutorial With Examples. Here in this blog post Coding compiler sharing a Python 3 virtual environments and packages tutorial for beginners. This Python tutorial is for beginners and intermediate learners who are looking to master in Python programming. Experienced Python programmers also can refer this tutorial to brush-up their Python 3 programming skills. Let’s start learning Python 3.
Python Virtual Environments and Packages
Let’s discuss in detail about each subtopic of Python virtual environment and packages.
- Python Virtual Environments and Packages Introduction
- Creating a Virtual Environment
- Using the pip Management Pack
1. Python Virtual Environments and Packages Introduction
Python applications often use packages and modules that are not part of the standard library. An application sometimes needs a particular version of a library because it requires a specific bug that has been fixed in the library or it is written using an interface to an outdated version of the library.
This means that it may not be possible to install a Python to meet the requirements of each application. If application A requires a version 1.0 of a particular module but application B requires version 2.0 of the module, the requirements of the two applications are conflicting. Installing version 1.0 or version 2.0 will cause one of the applications to fail.
Related Article: Python Interpreter Tutorial
The solution to this problem is to create a virtual environment (commonly referred to as “virtualenv”), it contains a specific version of Python, as well as a separate directory tree some additional packages.
Different applications can use different virtual environments. In order to resolve the conflict in the previous example, Application A can have its own virtual environment with version 1.0 of the specific module installed. Application B has another virtual environment with a specific module version 2.0 installed. If Application B requires a library upgrade to 3.0, this will not affect the environment of Application A.
Related Articles: Learn Python In One Day
2. Creating a Virtual Environment
The script for creating and managing virtual environments is called pyvenv . Pyvenv will usually install the latest version of Python available to you. This script can also specify the installation of a specific version of Python, so if there are multiple versions of Python on your system, you can run
pyvenv-3.5or any version you want to select a specific version of Python.
Related Article: Python For Machine Learning
To create a virtualenv, first determine the directory you want to store and run pyvenv followed by the directory name:
If the directory does not exist, it will create a
tutorial-envdirectory, and also create a directory containing Python interpreter, the standard library, and Python “copy” of a variety of supporting documents.
Once you have created a virtual environment, you must activate it.
On Windows, run:
On Unix or MacOS, run:
(The bash shell script is written in. If you are using csh or FISH shell, you should use
Related Article: Python Data Structures
Activate the virtual environment will change your shell prompt, display a virtual environment you are using, and modify the environment so that operation
pythonwill allow you to get a specific version of Python. E.g:
-> source ~/envs/tutorial-env/bin/activate (tutorial-env) -> python Python 3.5.2+ (3.4:c7b9645a6f35+, May 22 2015, 09:31:25) ... >>> import sys >>> sys.path ['', '/usr/local/lib/python35.zip', ..., '~/envs/tutorial-env/lib/python3.5/site-packages'] >>>
3. Using the pip Management Pack
Once you have activated a virtual environment, you can use a program called pip to install, upgrade, and delete packages. By default
pipit will be the Package Index from the Python, < https://pypi.python.org/pypi >, installation package.
Related Article: Python Modules
You can view them through a web browser, or you can use
pipa limited search function:
(tutorial-env) -> pip search astronomy skyfield - Elegant astronomy for Python gary - Galactic astronomy and gravitational dynamics. novas - The United States Naval Observatory NOVAS astronomy library astroobs - Provides astronomy ephemeris to plan telescope observations PyAstronomy - A collection of astronomy related tools for Python . ...
pipThere are many subcommands: “Search”, “Install”, “Uninstall”, “Freeze” (Translator’s Note: The phrase is temporarily not suitable for translation), and so on.
Related Article: Python Input and Output
You can install the latest version of a package by specifying the package name:
-> pip install novas Collecting novas Downloading novas-220.127.116.11.tar.gz (136kB) Installing collected packages: novas Running setup.py install for novas Successfully installed novas-18.104.22.168
You can also install a specific version of the package, followed by the package name given later
==and version number:
Related Article: Python Errors and Exceptions
-> pip install requests==2.6.0 Collecting requests==2.6.0 Using cached requests-2.6.0-py2.py3-none-any.whl Installing collected packages: requests Successfully installed requests-2.6.0
If you re-run the command (pip install requests==2.6.0),
pipyou will notice that the required version is already installed and will not do anything.
Related Article: Python Classes and Objects
You can also provide a different version number to install or run to upgrade to the latest version of the package:
pip install --upgrade
-> pip install --upgrade requests Collecting requests Installing collected packages: requests Found existing installation: requests 2.6.0 Uninstalling requests-2.6.0: Successfully uninstalled requests-2.6.0 Successfully installed requests-2.7.0
pip uninstall Subsequent to one or more package names will remove these packages from the virtual environment.
pip show The information for a specified package will be displayed:
(tutorial-env) -> pip show requests --- Metadata-Version: 2.0 Name: requests Version: 2.7.0 Summary: Python HTTP for Humans. Home-page: http://python-requests.org Author: Kenneth Reitz Author-email: email@example.com License: Apache 2.0 Location: /Users/akuchling/envs/tutorial-env/lib/python3.4/site-packages Requires:
pip list All packages installed in the virtual environment will be listed:
(tutorial-env) -> pip list novas (22.214.171.124) numpy (1.9.2) pip (7.0.3) requests (2.7.0) setuptools (16.0)
pip freezeIt will generate a list of similar packages need to be installed, but the output is the desired format. Common practice is to put them in a file:
Related Article: Python Standard Library – 1
(tutorial-env) -> pip freeze > requirements.txt (tutorial-env) -> cat requirements.txt novas==126.96.36.199 numpy==1.9.2 requests==2.7.0
requirements.txtCan be submitted to version control and as part of an application. Users can use to install all the necessary packages:
Related Article: Python Standard Library Part – 2
-> pip install -r requirements.txt Collecting novas==188.8.131.52 (from -r requirements.txt (line 1)) ... Collecting numpy==1.9.2 (from -r requirements.txt (line 2)) ... Collecting requests==2.7.0 (from -r requirements.txt (line 3)) ... Installing collected packages: novas, numpy, requests Running setup.py install for novas Successfully installed novas-184.108.40.206 numpy-1.9.2 requests-2.7.0
pipThere are more options. Please refer to the installing-index guide for information about
pipthe document intact. When you write a package and it also appears in the Python Package Index, refer to the distributing-index guide.