Bootstrap FreeKB - Python (Scripting) - Activate a virtual environment
Python (Scripting) - Activate a virtual environment

Updated:   |  Python (Scripting) articles

This assumes you have created a virtual environment. In this example, a virtual environment named foo was created.

Before activating a virtual environment, you can echo the $PATH variable to see the directories that are currently in your PATH.

echo $PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/john.doe/.local/bin:/home/john.doe/bin

 

Likewise, the which command should show the normal location of the python command.

which python
/usr/bin/python

which python3
/usr/bin/python3

 

Use the source command to add the virtual environment to your PATH.

source <base directory of your virtual environment>/bin/activate

 

Now, the name of the virtual environment will be displayed on the command line.

(foo) [~#]$

 

You can echo the $PATH and the virtual environment should be in your path.

echo $PATH
/home/john.doe/foo/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/john.doe/.local/bin:/home/john.doe/bin

 

Likewise, the which command should show that the python command is within the virtual environment.

(foo) [~#]$ which python
~/foo/bin/python

(foo) [~#]$ which python3
~/foo/bin/python3

 

Type deactivate to leave the virtual environment.

(foo) [~#]$ deactivate 

 

Now, the name of the virtual environment should no longer be displayed on the command line.

[~#]$



Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 58634d in the box below so that we can be sure you are a human.