Environments in Python
Python programs often use packages and components that are not included in the standard library. Applications will repeatedly need a new version of a library since the application will require that a particular error has been patched or because an outdated version of the library interface will be used to write the application.
It may not be possible for one installation of Python to satisfy each application’s specifications. If application A requires version 1.0 of a particular module, application B requires version 2.0, so there is a dispute between the specifications. If only version 1.0 or 2.0 is installed, one application will be unable to run.
Creating a virtual world, a self-contained directory tree that includes a Python installation for a specific Python version, plus various additional packages, is the solution to this issue.
Various systems will then use multiple virtual worlds. Application A will have its virtual environment with version 1.0 installed to address the previous case of conflicting specifications, while application B has another virtual environment with version 2.0.0. If application B allows the library to be updated to version 3.0, this does not change application A’s environment.
Create Virtual Environments
The module that is used for building and controlling virtual worlds is called venv. Venv will typically update the most current version of Python available to the user. User may choose a particular version of Python by running python3 or whichever version user determines if user have different versions of Python on the system.
To build a virtual environment, determine where to put a directory, and run the venv module as a script with the path to the directory:
python3 -m venv tutorial-env
If it does not exist, this would create the tutorial-env directory and create folders within it containing a replica of the Python paraphraser, and other files supporting it.
A standard directory location for a virtual world is .venv. Typically, this name holds the directory locked in the shell and thus out of the way by giving it a name that explains why there is a directory. It also avoids clashing with .env environment variable specification files that are supported by particular software.
Activating the virtual environment will adjust the shell’s prompt to display which virtual environment the user is using and alter the environment so that the specific version and installation of Python is accessible to the user to run Python. For instance:
$ source ~/envs/tutorial-env/bin/activate (tutorial-env) $ python Python 3.5.1 (default, May 6 2016, 10:59:36) ... >>> import sys >>> sys.path ['', '/usr/local/lib/python35.zip', ..., '~/envs/tutorial-env/lib/python3.5/site-packages'] >>>
Packages Managing with PIP
Using a software named pip, user may load, update, and uninstall packages. By default, pip installs the packages from the Python Index, <https://pypi.org>. It redirects to the web browser, user may browse the Python Package Index, or use pip’s narrow search feature:
There are several subcommands in pip: “search,” “install,” “uninstall,” “freeze,” etc. (Consult the Installing Python Modules guide for complete documentation for pip.)
$ python -m pip install novas Collecting novas Downloading novas-188.8.131.52.tar.gz (136kB) Installing collected packages: novas Running setup.py install for novas
Pip will find that the requested version is already installed and will do nothing if user re-run this order. To get the update, user can have a different version number or run pip install —upgrade to upgrade the software to the new version.
|pip install||Installs the packages in the virtual environment.|
|pip uninstall||Remove the packages from the virtual environment.|
|pip show||Displays information about a particular package|
|pip list||Displays all of the packages installed in the virtual environment.|
|pip freeze||Produces a similar list of the installed packages.|
Other useful articles:
- OOP in Python
- Python v2 vs Python v3
- Variables, Data Types, and Syntaxes in Python
- Operators, Booleans, and Tuples
- Loops and Statements in Python
- Python Functions and Modules
- Regular Expressions in Python
- Python Interfaces
- JSON Data and Python
- Pip and its Uses in Python
- File Handling in Python
- Searching and Sorting Algorithms in Python
- System Programming (Pipes &Threads etc.)
- Database Programming in Python
- Debugging with Assertion in Python
- Sockets in Python
- InterOp in Python
- Exception Handling in Python
- Environments in Python
- Foundation of Data Science
- Reinforcement Learning
- Python for AI
- Applied Text Mining in Python
- Python Iterations using Libraries
- NumPy vs SciPy
- Python Array Indexing and Slicing
- Python & Libraries