Virtual Environment for Python Workspace Part 3/3 venv
In the earlier two posts of this series, I discussed about virtualenv
and conda
and their usage along with some examples. I this post I am going to introduce you guys with another package and environment manager called venv
which comes with standard Python3.X by default. So, nothing to worry about its installation.
Virtual Environment for Python Workspace — Part 1⁄3 — virtualenv
Virtual Environment for Python Workspace — Part 2⁄3 — conda
Creating an Environment
For example, If you have Python3.X installed in your system and you can run it’s REPL by issuing python3
in the terminal, then you can create a virtual environment based on this Python version by issuing the following command:
python3 -m venv myvenv
This will create the myvenv
directory if it doesn't exist, and also create directories inside it containing a copy of the Python interpreter, the standard library, and various supporting files. By the way, you got the idea of using different versions of Python to use its venv
module for creating different environments, right? Just find out an exact Python 3.X (I showed how, in earlier posts) and use its binary/executable in the command <active python> -m venv <environment name>
.
Activating the Environment
It’s similar to the previous two workarounds and again simple as below. I assumed you are inside the newly-created myvenv
directory.
source bin/activate
Managing Packages with pip
As venv
is native with Python, you can use pip
for installing packages in your virtual environment which will pull down packages from Python's official package repository, PyPI. For example, to install our very favorite package numpy
, use the following command,
pip install numpy
Also, for sure you can use commands like pip search <packagename>
, pip list
in each newly created virtual environment.
Did you know, you can use pip show <packagename>
to see detail of an installed package in an active environment?