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.

[andy@home-pc ~]$ python --version
Python 3.9.7

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

[andy@home-pc ~]$ pamac install python2

The virtualenvwrapper package is available from the official repository.

[andy@home-pc ~]$ pamac search python-virtualenvwrapper
python-virtualenvwrapper                                                                                                4.8.4-5  community 
    Extensions to Ian Bicking's virtualenv tool

Install it with.

[andy@home-pc ~]$ pamac install python-virtualenvwrapper

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

[andy@home-pc ~]$ mkdir -v ~/.virtualenvs
mkdir: created directory '/home/andy/.virtualenvs'
[andy@home-pc ~]$ 
[andy@home-pc ~]$  export WORKON_HOME=~/.virtualenvs
[andy@home-pc ~]$ 
[andy@home-pc ~]$ 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.

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

You will also need to set the VIRTUALENVWRAPPER_PYTHON environment variable.

[andy@home-pc ~]$ 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.

[andy@home-pc ~]$ 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.

[andy@home-pc ~]$ 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:

[andy@home-pc ~]$ 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) [andy@home-pc my_python3_project]$

Use the deactivate command to exit the virtual python environment.

(my_python3_project) [andy@home-pc my_python3_project]$ deactivate
[andy@home-pc my_python3_project]$ cd
[andy@home-pc ~]$

To create a python2 virtual environment.

[andy@home-pc ~]$ 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) [andy@home-pc my_python2_project]$

Again – deactivate to exit the python2 virtual environment.

(my_python2_project) [andy@home-pc my_python2_project]$ deactivate 
[andy@home-pc my_python2_project]$ cd
[andy@home-pc ~]$ 

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

[andy@home-pc ~]$ 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.

[andy@home-pc ~]$ workon my_python3_project
(my_python3_project) [andy@home-pc my_python3_project]$ 

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

[andy@home-pc ~]$ rmvirtualenv my_python2_project
Removing my_python2_project...
[andy@home-pc ~]$ 
[andy@home-pc ~]$ workon
my_python3_project

And that’s it. Happy coding!

Be the first to comment

Leave a Reply