Python virtual environments using virtualenvwrapper on Manjaro i3 CE

If you’re looking to do the same thing on Ubuntu 18.04 – checkout my previous post.

Check the python version – hopefully you’ve got the latest 3.x version.

[[email protected] ~]$ python --version
Python 3.9.7

Python2 is now deprecated but you’ll need it if you want to run any python2 applications.

[[email protected] ~]$ pamac install python2

The virtualenvwrapper package is available from the official repository.

[[email protected] ~]$ pamac search python-virtualenvwrapper
python-virtualenvwrapper                                                                                                4.8.4-5  community 
    Extensions to Ian Bicking's virtualenv tool

Install it with.

[[email protected] ~]$ pamac install python-virtualenvwrapper

Create and set the directory you want to keep your virtual environments in.

[[email protected] ~]$ mkdir -v ~/.virtualenvs
mkdir: created directory '/home/andy/.virtualenvs'
[[email protected] ~]$ 
[[email protected] ~]$  export WORKON_HOME=~/.virtualenvs
[[email protected] ~]$ 
[[email protected] ~]$ source /usr/bin/virtualenvwrapper.sh 
virtualenvwrapper.user_scripts creating /home/andy/.virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /home/andy/.virtualenvs/postmkproject
virtualenvwrapper.user_scripts creating /home/andy/.virtualenvs/initialize
virtualenvwrapper.user_scripts creating /home/andy/.virtualenvs/premkvirtualenv
virtualenvwrapper.user_scripts creating /home/andy/.virtualenvs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /home/andy/.virtualenvs/prermvirtualenv
virtualenvwrapper.user_scripts creating /home/andy/.virtualenvs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /home/andy/.virtualenvs/predeactivate
virtualenvwrapper.user_scripts creating /home/andy/.virtualenvs/postdeactivate
virtualenvwrapper.user_scripts creating /home/andy/.virtualenvs/preactivate
virtualenvwrapper.user_scripts creating /home/andy/.virtualenvs/postactivate
virtualenvwrapper.user_scripts creating /home/andy/.virtualenvs/get_env_details

I want my projects (source) to be created in the ~/Devel directory.

[[email protected] ~]$ mkdir -v ~/Devel
mkdir: created directory '/home/andy/Devel'
[[email protected] ~]$ 
[[email protected] ~]$ export PROJECT_HOME=$HOME/Devel

You will also need to set the VIRTUALENVWRAPPER_PYTHON environment variable.

[[email protected] ~]$ export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python

That should all now be working. However, to make it permanent, you’ll need to set those environment variables on boot.

[[email protected] ~]$ echo -en '\n\n#virtualenvwrapper\nexport VIRTUALENVWRAPPER_PYTHON=/usr/bin/python\nexport WORKON_HOME=$HOME/.virtualenvs\nsource /usr/bin/virtualenvwrapper.sh\nexport PROJECT_HOME=$HOME/Devel\n' >> ~/.bashrc

Then source the ~/.bashrc file.

[[email protected] ~]$ source ~/.bashrc

Putting it to the test

If all went well, you should be able to create a new project like so. To create (the default) python3 project:

[[email protected] ~]$ mkproject my_python3_project
...
Creating /home/andy/Devel/my_python3_project
Setting project for my_python3_project to /home/andy/Devel/my_python3_project
(my_python3_project) [[email protected] my_python3_project]$

Use the deactivate command to exit the virtual python environment.

(my_python3_project) [[email protected] my_python3_project]$ deactivate
[[email protected] my_python3_project]$ cd
[[email protected] ~]$

To create a python2 virtual environment.

[[email protected] ~]$ mkproject -p /usr/bin/python2.7 my_python2_project
created virtual environment CPython2.7.18.final.0-64 in 162ms
...
Creating /home/andy/Devel/my_python2_project
Setting project for my_python2_project to /home/andy/Devel/my_python2_project
(my_python2_project) [[email protected] my_python2_project]$

Again – deactivate to exit the python2 virtual environment.

(my_python2_project) [[email protected] my_python2_project]$ deactivate 
[[email protected] my_python2_project]$ cd
[[email protected] ~]$ 

You can list your virtual environment with lsvirtualenv or workon (without any options).

[[email protected] ~]$ lsvirtualenv 
my_python2_project
==================


my_python3_project
==================

To resume work on one of your projects, use the workon command with the name of the project, like so.

[[email protected] ~]$ workon my_python3_project
(my_python3_project) [[email protected] my_python3_project]$ 

Lastly, to remove (and delete) a virtual environment, use the rmvirtualenv command.

[[email protected] ~]$ rmvirtualenv my_python2_project
Removing my_python2_project...
[[email protected] ~]$ 
[[email protected] ~]$ workon
my_python3_project

And that’s it. Happy coding!

Be the first to comment

Leave a Reply