How to activate virtualenv on Linux?

LinuxOpen SourceOperating System

When we talk about keeping our dependencies in a separate location from our logic code we are indeed creating nothing but a virtual environment, which in python, we usually use the term venv to refer to as.

So a venv is nothing but a virtual environment which is in turn a tool that allows us to keep our dependencies that are required by the project to be kept in a separate folder. These separate folders that we create are known as the python virtual environments.

Python venv is one of the most widely used tools.

Now we know what virtualenv is and what it is used for, let’s see how we can create one in python on linux and what characteristics and features does it offer.

In simpler terms, the venv tool is nothing but a module in python and it is used to provide support in creating “virtual environments” that are both leightweights and contain their own site directories. These virtual environments are isolated from the system's site directories. It should also be good to note that these virtual environments have their own python binaries and can also have their own set of python packages that came already installed in their site directories.

Creating virtual environments

Creating a virtual environment can be done using the command shown below −

python3 -m venv /path_to_new_virtual_environment

Now let’s run the above command on a Unix environment, I am using Mac OS and the command will look something like this −

python3 -m venv /Users/immukul/linux-questions-code

After running the command, you won’t get any message, instead the terminal will be back where it started from and now you just need to locate the directory in which you create a virtual environment and inside that directory you must have files that are similar or same to the output shown below −

immukul@192 linux-questions-code % ls -ltr
total 16
-rw-r--r-- 1 immukul staff 28 Jul 4 13:33 textfile.txt
drwxr-xr-x 2 immukul staff 64 Jul 5 20:52 include
drwxr-xr-x 3 immukul staff 96 Jul 5 20:52 lib
-rw-r--r-- 1 immukul staff 90 Jul 5 20:52 pyvenv.cfg
drwxr-xr-x 12 immukul staff 384 Jul 5 20:52 bin

That is how to create a virtualenv in Linux. There are certain arguments that the venv module allows us to use and run and these mainly can be obtained by writing the following command to the terminal −

python3 -m venv

This command will output all the positional arguments along with the optional arguments that you can use in the venv module.

Output

usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
   [--upgrade] [--without-pip] [--prompt PROMPT] [--upgrade-deps]
   ENV_DIR [ENV_DIR ...]
venv: error: the following arguments are required: ENV_DIR

immukul@192 ~ % python3 -m venv -h

usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
   [--upgrade] [--without-pip] [--prompt PROMPT] [--upgrade-deps]
   ENV_DIR [ENV_DIR ...]

Creates virtual Python environments in one or more target directories.

Positional arguments:
   ENV_DIR A directory to create the environment in.
Optional arguments:
   -h, --help Show this help message and exit
   --system-site-packages
                  Give the virtual environment access to the system site-packages dir.
--symlinks Try to use symlinks rather than copies, when symlinks are not the default for the platform.
--copies Try to use copies rather than symlinks, even when symlinks are the default for the platform.
--clear Delete the contents of the environment directory if it already exists, before environment creation.
--upgrade Upgrade the environment directory to use this version of Python, assuming Python has been upgraded in-place.
--without-pip Skips installing or upgrading pip in the virtual environment (pip is bootstrapped by default)
--prompt PROMPT Provides an alternative prompt prefix for this environment. upgrade-deps Upgrade core dependencies: pip setup tools to the latest version in PyPI
raja
Published on 29-Jul-2021 11:37:37
Advertisements