Get Virtual! with Python Virtual Environments

These are used to isolate environments and resolve dependency conflict resolution.

This ensure projects have isolated siloes where they live and have their own dependency tree that don’t interfere with one another.

Another thing, feel free to jump to sections that you’re only interested in!


Python Dependencies

You will often use 3rd-party libraries – non-default libraries which doesn’t come along with Python.
You’ll have to import and install them before they can be used.

  • normally installed using pip or easy_install
  • libraries are pulled from the pupi.org or Python Package Index which as an enormous index of libraries


Installing Libraries

To install a library, as example, django

pip <span>install </span>django
pip <span>install </span>django
pip install django

Enter fullscreen mode Exit fullscreen mode

To install a specific version of django,

pip <span>install </span><span>django</span><span>==</span>2.2.12
pip <span>install </span><span>django</span><span>==</span>2.2.12
pip install django==2.2.12

Enter fullscreen mode Exit fullscreen mode

To check the version of Django installed, you can run any of the commands below:

django <span>--version</span>
python <span>-m</span> django <span>--version</span>
django <span>--version</span>
python <span>-m</span> django <span>--version</span>
django --version python -m django --version

Enter fullscreen mode Exit fullscreen mode

You can also view the versions of all installed packages, including Django,

pip freeze
python <span>-m</span> pip freeze
pip freeze
python <span>-m</span> pip freeze
pip freeze python -m pip freeze

Enter fullscreen mode Exit fullscreen mode

To save all of these data (versions of each modules) to be reused or processed later, you can forward them to a file.

pip freeze <span>></span> module-versions.txt
pip freeze <span>></span> module-versions.txt
pip freeze > module-versions.txt

Enter fullscreen mode Exit fullscreen mode

To upgrade to a newer version

pip <span>install</span> <span>--upgrade</span> django
pip <span>install</span> <span>--upgrade</span> django
pip install --upgrade django

Enter fullscreen mode Exit fullscreen mode

To uninstall it, don’t delete the folder, instead

pip uninstall django
pip uninstall django
pip uninstall django

Enter fullscreen mode Exit fullscreen mode

Another example: installing the “site” library

import site
print<span>(</span>site.getsitepackages<span>())</span>
import site
print<span>(</span>site.getsitepackages<span>())</span>
import site print(site.getsitepackages())

Enter fullscreen mode Exit fullscreen mode


Using a requirements.txt

Another way to install multiple third parties with just a single command is to put them into a single requirements.txt file and use pip to to do a bulk-install.

(Note that it’s recommended to install modules for a project in a virtual environment. Read below to learn more about this.)

To install from a requirements file

pip <span>install</span> <span>-r</span> requirements.txt
pip <span>install</span> <span>-r</span> requirements.txt
pip install -r requirements.txt

Enter fullscreen mode Exit fullscreen mode


Using env

Let’s take django as our example again. We recently installed django version 2.2.12. If we are to install a new version, v3.0, then it will override and uninstall the v2.2.12.

Some projects need specific versions of a library and this is where virtual environments come into play. Virtual environments exist to isolate projects and their dependencies from one another.

To create a virtual environment, we can use venv

python <span>-m</span> venv my-project-1
python <span>-m</span> venv my-project-1
python -m venv my-project-1

Enter fullscreen mode Exit fullscreen mode

After you run this, a folder for the virtual environment will be created.

<span>$ </span>ll
total 0
drwxr-xr-x 1 Eden Jose 197610 0 Sep 14 12:16 my-project-1/
<span>$ </span>ll my-project-1/
total 5
drwxr-xr-x 1 Eden Jose 197610 0 Sep 14 12:16 Include/
drwxr-xr-x 1 Eden Jose 197610 0 Sep 14 12:16 Lib/
<span>-rw-r--r--</span> 1 Eden Jose 197610 121 Sep 14 12:16 pyvenv.cfg
drwxr-xr-x 1 Eden Jose 197610 0 Sep 14 12:16 Scripts/
<span>$ </span>ll
total 0
drwxr-xr-x 1 Eden Jose 197610 0 Sep 14 12:16 my-project-1/

<span>$ </span>ll my-project-1/
total 5
drwxr-xr-x 1 Eden Jose 197610   0 Sep 14 12:16 Include/
drwxr-xr-x 1 Eden Jose 197610   0 Sep 14 12:16 Lib/
<span>-rw-r--r--</span> 1 Eden Jose 197610 121 Sep 14 12:16 pyvenv.cfg
drwxr-xr-x 1 Eden Jose 197610   0 Sep 14 12:16 Scripts/
$ ll total 0 drwxr-xr-x 1 Eden Jose 197610 0 Sep 14 12:16 my-project-1/ $ ll my-project-1/ total 5 drwxr-xr-x 1 Eden Jose 197610 0 Sep 14 12:16 Include/ drwxr-xr-x 1 Eden Jose 197610 0 Sep 14 12:16 Lib/ -rw-r--r-- 1 Eden Jose 197610 121 Sep 14 12:16 pyvenv.cfg drwxr-xr-x 1 Eden Jose 197610 0 Sep 14 12:16 Scripts/

Enter fullscreen mode Exit fullscreen mode

To activate a virtual environment, run the “activate” script inside the Scripts folder of the project. As an example,

<span>$ </span><span>source</span> ./my-project-1/Scripts/activate
<span>$</span>
<span>(</span>my-project-1<span>)</span>
Eden Jose@EdenJose MINGW64 ~/Desktop/Git/5-Virtual-Envs
<span>$ </span><span>source</span> ./my-project-1/Scripts/activate
<span>$</span>
<span>(</span>my-project-1<span>)</span> 
Eden Jose@EdenJose MINGW64 ~/Desktop/Git/5-Virtual-Envs
$ source ./my-project-1/Scripts/activate $ (my-project-1) Eden Jose@EdenJose MINGW64 ~/Desktop/Git/5-Virtual-Envs

Enter fullscreen mode Exit fullscreen mode

Notice that the name of the virtual environment can now be seen at the prompt. To exit out of the virtual environment, simply run

deactivate
deactivate
deactivate

Enter fullscreen mode Exit fullscreen mode


Using venv

Another way to create virtual environments is through virtual env.
Note that virtual env doesn’t ship alongside your Python installation. To install virtual env,

pip <span>install </span>virtualenv
pip <span>install </span>virtualenv
pip install virtualenv

Enter fullscreen mode Exit fullscreen mode

This take a similar step to create a new environment and activate it,

virtualenv my-project-2
<span># you can also use</span>
python <span>-m</span> virtualenv my-project-2
virtualenv my-project-2

<span># you can also use</span>
python <span>-m</span> virtualenv my-project-2
virtualenv my-project-2 # you can also use python -m virtualenv my-project-2

Enter fullscreen mode Exit fullscreen mode

You can create the virtual environment with a different Python installation by using the “-p” flag

<span># Check the python installations you currently have</span>
<span>$ </span>where python
C:<span>\U</span>sers<span>\E</span>den Jose<span>\A</span>ppData<span>\L</span>ocal<span>\P</span>rograms<span>\P</span>ython<span>\P</span>ython39<span>\p</span>ython.exe
C:<span>\U</span>sers<span>\E</span>den Jose<span>\A</span>ppData<span>\L</span>ocal<span>\P</span>rograms<span>\P</span>ython<span>\P</span>ython38<span>\p</span>ython.exe
<span># You can choose from this two and create a virtual env with that version</span>
python <span>-m</span> virtualenv <span>-p</span>
<span># Check the python installations you currently have</span>
<span>$ </span>where python
C:<span>\U</span>sers<span>\E</span>den Jose<span>\A</span>ppData<span>\L</span>ocal<span>\P</span>rograms<span>\P</span>ython<span>\P</span>ython39<span>\p</span>ython.exe
C:<span>\U</span>sers<span>\E</span>den Jose<span>\A</span>ppData<span>\L</span>ocal<span>\P</span>rograms<span>\P</span>ython<span>\P</span>ython38<span>\p</span>ython.exe

<span># You can choose from this two and create a virtual env with that version</span>
python <span>-m</span> virtualenv <span>-p</span>
# Check the python installations you currently have $ where python C:\Users\Eden Jose\AppData\Local\Programs\Python\Python39\python.exe C:\Users\Eden Jose\AppData\Local\Programs\Python\Python38\python.exe # You can choose from this two and create a virtual env with that version python -m virtualenv -p

Enter fullscreen mode Exit fullscreen mode


Using virtualenvwrapper

This is what I am using in labs and even at work. Treat this section as standalone, and can be setup even without the venv or env

This is another virtualenv library which wraps up some useful management functionality for virtualenv. One feature of this is it manages a single location of all your projects.

Unlike virtualenv and env where the project folder is created on your current working directory, virtualenvwrapper creates a folder is the user’s home directory.

virtualenvwrapper maintains this folder where all your environment folders are created by default. You can setup your own directory where all the virtual environments folder will be created by creating the variable WORKON_HOME.

I had some problems when I was trying this one. As a solution, I just uninstalled any existing virtualenvwrapper installed on my system and do a fresh install. This

To do a fresh install,

joseeden@EdenJose:~<span>$ </span><span>sudo </span>pip uninstall virtualenvwrapper
joseeden@EdenJose:~<span>$ </span><span>sudo </span>pip <span>install </span>virtualenvwrapper
joseeden@EdenJose:~<span>$ </span><span>sudo </span>pip uninstall virtualenvwrapper
joseeden@EdenJose:~<span>$ </span><span>sudo </span>pip <span>install </span>virtualenvwrapper
joseeden@EdenJose:~$ sudo pip uninstall virtualenvwrapper joseeden@EdenJose:~$ sudo pip install virtualenvwrapper

Enter fullscreen mode Exit fullscreen mode

Next, append this to your .bashrc file

<span># Change the WORKON_HOME path to your directory where you want all your virtual environment folders created</span>
<span>export </span><span>WORKON_HOME</span><span>=</span><span>'/mnt/c/Users/Eden Jose/Desktop/Git/5-Virtual-Envs'</span>
<span>export </span><span>VIRTUALENVWRAPPER_PYTHON</span><span>=</span><span>'/usr/bin/python3'</span>
<span>source</span> /usr/local/bin/virtualenvwrapper.sh
<span># Change the WORKON_HOME path to your directory where you want all your virtual environment folders created</span>
<span>export </span><span>WORKON_HOME</span><span>=</span><span>'/mnt/c/Users/Eden Jose/Desktop/Git/5-Virtual-Envs'</span>
<span>export </span><span>VIRTUALENVWRAPPER_PYTHON</span><span>=</span><span>'/usr/bin/python3'</span>
<span>source</span> /usr/local/bin/virtualenvwrapper.sh
# Change the WORKON_HOME path to your directory where you want all your virtual environment folders created export WORKON_HOME='/mnt/c/Users/Eden Jose/Desktop/Git/5-Virtual-Envs' export VIRTUALENVWRAPPER_PYTHON='/usr/bin/python3' source /usr/local/bin/virtualenvwrapper.sh

Enter fullscreen mode Exit fullscreen mode

To create a virtual environment

mkvirtualenv <name>
<span># Sample</span>
joseeden@EdenJose:~<span>$ </span>mkvirtualenv project-a
created virtual environment CPython3.8.10.final.0-64 <span>in </span>21319ms...
<span>(</span>project-a<span>)</span> joseeden@EdenJose:~<span>$</span>
<span># Note that to create another virtual envionment, exit out of the previous virtual environment by running "deactivate"</span>
joseeden@EdenJose:~<span>$ </span>mkvirtualenv project-b
created virtual environment CPython3.8.10.final.0-64 <span>in </span>21319ms...
<span>(</span>project-b<span>)</span> joseeden@EdenJose:~<span>$</span>
mkvirtualenv <name>

<span># Sample</span>
joseeden@EdenJose:~<span>$ </span>mkvirtualenv project-a
created virtual environment CPython3.8.10.final.0-64 <span>in </span>21319ms...
<span>(</span>project-a<span>)</span> joseeden@EdenJose:~<span>$</span>

<span># Note that to create another virtual envionment, exit out of the previous virtual environment by running "deactivate"</span>

joseeden@EdenJose:~<span>$ </span>mkvirtualenv project-b
created virtual environment CPython3.8.10.final.0-64 <span>in </span>21319ms...
<span>(</span>project-b<span>)</span> joseeden@EdenJose:~<span>$</span>
mkvirtualenv <name> # Sample joseeden@EdenJose:~$ mkvirtualenv project-a created virtual environment CPython3.8.10.final.0-64 in 21319ms... (project-a) joseeden@EdenJose:~$ # Note that to create another virtual envionment, exit out of the previous virtual environment by running "deactivate" joseeden@EdenJose:~$ mkvirtualenv project-b created virtual environment CPython3.8.10.final.0-64 in 21319ms... (project-b) joseeden@EdenJose:~$

Enter fullscreen mode Exit fullscreen mode

To see all your virtual environments,

joseeden@EdenJose:~<span>$ </span>workon
project-a
project-b
joseeden@EdenJose:~<span>$ </span>workon
project-a
project-b
joseeden@EdenJose:~$ workon project-a project-b

Enter fullscreen mode Exit fullscreen mode

To switch between virtual environments, you can simply run

joseeden@EdenJose:~<span>$ </span>workon project-b
<span>(</span>project-b<span>)</span> joseeden@EdenJose:~<span>$</span>
joseeden@EdenJose:~<span>$ </span>workon project-b
<span>(</span>project-b<span>)</span> joseeden@EdenJose:~<span>$</span>
joseeden@EdenJose:~$ workon project-b (project-b) joseeden@EdenJose:~$

Enter fullscreen mode Exit fullscreen mode

To exit out of a virtual environment

<span>(</span>project-b<span>)</span> joseeden@EdenJose:~<span>$ </span>deactivate
joseeden@EdenJose:~<span>$</span>
<span>(</span>project-b<span>)</span> joseeden@EdenJose:~<span>$ </span>deactivate
joseeden@EdenJose:~<span>$</span>
(project-b) joseeden@EdenJose:~$ deactivate joseeden@EdenJose:~$

Enter fullscreen mode Exit fullscreen mode


ERRORS

mkvirtualenv: command not found

If you get an error “command not found”, this might mean virtualenvwrapper was not properly installed.

You can simply re-do the installation.
I had issues also when trying the virtualenvwrapper on Git Bash in VSCode so I decided to run the commands below in WSL.

<span>sudo </span>pip uninstall virtualenv <span>-y</span>
<span>sudo </span>pip uninstall virtualenvwrapper <span>-y</span>
<span>sudo </span>pip <span>install </span>virtualenv
<span>sudo </span>pip <span>install </span>virtualenvwrapper
<span>echo</span> <span>"WORKON_HOME='/mnt/c/Users/Eden Jose/Desktop/Git/5-Virtual-Envs'"</span> <span>>></span> ~/.bashrc
<span>echo</span> <span>"source </span><span>`</span>which virtualenvwrapper.sh<span>`</span><span>"</span> <span>>></span> ~/.bashrc
<span>.</span> ~/.bashrc
<span>sudo </span>pip uninstall virtualenv <span>-y</span> 
<span>sudo </span>pip uninstall virtualenvwrapper <span>-y</span>
<span>sudo </span>pip <span>install </span>virtualenv
<span>sudo </span>pip <span>install </span>virtualenvwrapper
<span>echo</span> <span>"WORKON_HOME='/mnt/c/Users/Eden Jose/Desktop/Git/5-Virtual-Envs'"</span> <span>>></span> ~/.bashrc
<span>echo</span> <span>"source </span><span>`</span>which virtualenvwrapper.sh<span>`</span><span>"</span> <span>>></span> ~/.bashrc
<span>.</span> ~/.bashrc
sudo pip uninstall virtualenv -y sudo pip uninstall virtualenvwrapper -y sudo pip install virtualenv sudo pip install virtualenvwrapper echo "WORKON_HOME='/mnt/c/Users/Eden Jose/Desktop/Git/5-Virtual-Envs'" >> ~/.bashrc echo "source `which virtualenvwrapper.sh`" >> ~/.bashrc . ~/.bashrc

Enter fullscreen mode Exit fullscreen mode


virtualenvwrapper Command ” not found, but can be installed with …

When you create a virtual environment using the mkvirtualenv command, you might see this error message

joseeden@EdenJose:/mnt/c/Users/Eden Jose<span>$ </span>mkvirtualenv project-a
Command <span>''</span> not found, but can be installed with:
<span>sudo </span>apt <span>install </span>mailutils-mh <span># version 1:3.7-2.1, or</span>
<span>sudo </span>apt <span>install </span>meshio-tools <span># version 4.0.4-1</span>
<span>sudo </span>apt <span>install </span>mmh <span># version 0.4-2</span>
<span>sudo </span>apt <span>install </span>nmh <span># version 1.7.1-6</span>
<span>sudo </span>apt <span>install </span>termtris <span># version 1.3-1</span>
joseeden@EdenJose:/mnt/c/Users/Eden Jose<span>$ </span>mkvirtualenv project-a

Command <span>''</span> not found, but can be installed with:

<span>sudo </span>apt <span>install </span>mailutils-mh  <span># version 1:3.7-2.1, or</span>
<span>sudo </span>apt <span>install </span>meshio-tools  <span># version 4.0.4-1</span>
<span>sudo </span>apt <span>install </span>mmh           <span># version 0.4-2</span>
<span>sudo </span>apt <span>install </span>nmh           <span># version 1.7.1-6</span>
<span>sudo </span>apt <span>install </span>termtris      <span># version 1.3-1</span>
joseeden@EdenJose:/mnt/c/Users/Eden Jose$ mkvirtualenv project-a Command '' not found, but can be installed with: sudo apt install mailutils-mh # version 1:3.7-2.1, or sudo apt install meshio-tools # version 4.0.4-1 sudo apt install mmh # version 0.4-2 sudo apt install nmh # version 1.7.1-6 sudo apt install termtris # version 1.3-1

Enter fullscreen mode Exit fullscreen mode

To resolve this, add the lins below (in this order) to your .bashrc file

<span>export </span><span>VIRTUALENVWRAPPER_PYTHON</span><span>=</span><span>'/usr/bin/python3'</span>
<span>source</span> /usr/local/bin/virtualenvwrapper.sh
<span>## Note that the path depends on where your Python is installed. To check</span>
which python
which python3
<span>export </span><span>VIRTUALENVWRAPPER_PYTHON</span><span>=</span><span>'/usr/bin/python3'</span>
<span>source</span> /usr/local/bin/virtualenvwrapper.sh

<span>## Note that the path depends on where your Python is installed. To check</span>
which python
which python3
export VIRTUALENVWRAPPER_PYTHON='/usr/bin/python3' source /usr/local/bin/virtualenvwrapper.sh ## Note that the path depends on where your Python is installed. To check which python which python3

Enter fullscreen mode Exit fullscreen mode


References


原文链接:Get Virtual! with Python Virtual Environments

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享
I love you for my life past.
我爱你,爱了整整一个曾经
评论 抢沙发

请登录后发表评论

    暂无评论内容