Ultimate magazine theme for WordPress.

Activate Venv: How To Create, Activate, Deactivate, And Delete Python Venv

Python apps often use non-standard packages and modules. Applications may need a specific version of a library because they require a bug fix or were written with an outdated library interface.

One Python installation may not meet all application needs. If application A needs version 1.0 of a module and application B needs version 2.0, the requirements conflict, and installing either version will make one application unusable.

Make a virtual environment, which is a self-contained directory tree with Python and other packages.

Various apps can use virtual environments. Application A can have a virtual environment with version 1.0 while Application B has version 2.0. Application B’s library upgrade won’t affect Application A’s environment.

Creating Virtual Environments

Van manages virtual environments. venv installs the latest Python version. If you have multiple Python versions on your system, run python3 or another version.

To create a virtual environment, run the venv module as a script with the directory path: python3 -m venv tutorial-env

This creates the tutorial-env directory if it doesn’t exist and adds Python and other files to it.

Virtual environment directories are denoted by. venv.This name hides the directory in your shell and explains its purpose. It prevents clashes with.env the environment variable definition files.

Activate a virtual environment after creating it.

On Windows, run: tutorial-env\Scripts\activate.bat

On Unix or macOS, run: source tutorial-env/bin/activate

This script is written for the bash shell. If you use the csh or fish shells, there are alternate activate. csh and activate. fish scripts you should use instead.

Activating the virtual environment will change your shell’s prompt to show what virtual environment you’re using and modify the environment so that running python will get you that particular version and installation of Python. For example,

$ 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]
>>>

Activate Venv:

Run this code. It will get activated if you are on a Windows machine.

source venv/Scripts/activate

Activate Venv

Run this code. It will get activated if you are on a Linux or Mac machine.

 venv/bin/activate

For Windows, You can perform as:

python=python.exe (if you do not create an environment variable).

The new Python works fine.

Managing Packages with pip

pip installs, upgrades, and removes packages. Pip installs packages from https://pypi.org/by default. Web browsers can access the Python Package Index.

“install,” “uninstall,” “freeze,” etc. are pip subcommands.

Specifying a package’s name installs the latest version.

(tutorial-env) $ python -m pip install novas
Collecting novas
Downloading novas-3.1.1.3.tar.gz (136kB)
Installing collected packages: novas
Running setup.py install for novas
Successfully installed novas-3.1.1.3

You can install a specific package version by using == and the package name.

(tutorial-env) $ python -m 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 rerun this command, pip will notice that the requested version is already installed. You can specify a different version number or run pip install —upgrade to upgrade.

(tutorial-env) $ python -m 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 followed by one or more package names removes them.

Pip show displays package information.

(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 protected]
License: Apache 2.0
Location: /Users/Kuching/envs/tutorial-env/lib/python3.4/site-packages
 pip list displays virtual environment packages:

(tutorial-env) $ pip list
novas (3.1.1.3)
NumPy (1.9.2)
pip (7.0.3)
requests (2.7.0)
setuptools (16.0)

pip freeze produces a similar list of installed packages but in pip install’s format. This list is usually in a requirements.txt file.

(tutorial-env) $ pip freeze > requirements.txt
(tutorial-env) $ cat requirements.txt
novas==3.1.1.3
numpy==1.9.2
requests==2.7.0

The requirements.txt file can be versioned and shipped with an app. Install-r lets users install all the required packages.

(tutorial-env) $ python -m pip install -r requirements.txt
Collecting novas==3.1.1.3 (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-3.1.1.3 NumPy-1.9.2 requests-2.7.0

pip is flexible. For pip documentation, see Installing Python Modules.

Read More: Watch.Salemnow.Com/Activate: How To Activate SalemNOW App?