Python Virtualenvwrapper Tool đ
Iâd like to introduce you to a cool tool that you can use to manage Python project virtual environments, called virtualenvwrapper
.
These are the slides from a Lightning talk I gave at the San Diego Python Meetup in March 2016.
You are probably already aware of virtual environments, or âvirtualenvsâ that allow you to use different versions of Python and Python packages. Last fall, when I was working with Trey Hunner with materials for the Python class he was teaching, I discovered the existence of virtualenvwrapper
and was excited to find out how useful it can be.
I try to be organized, and keep my projects and virtualenvs where I can find them. But I was finding it difficult to remember what I named projects and venvs. Especially when following various tutorials that do things a little differently.
Now with virtualenvwrapper
, I can always find my projects and virtualenvs.
Step 1. Install It Globally
My default Python is 2.7, but I want to have my default Python projects be 3.5, so I used
pip3.5
instead of just pip
.
Step 2. Simple Set-up
Once all this is set up, you donât need to worry about it anymore. Note: My .bashrc file is sourced by .bash_profile
Step 3. Create a Project
Just like with any virtualenvs, your command line will be prefixed by the name of the virtualenv.
Later onâŚ
The best part comes later, when youâve been away from the project, the terminal windowâs been closed, and youâve forgotten the details of the project. Say you called the project newapp
when you created it with the virtualenvwrapper
tool. All you have to do to get back to it is type workon newapp
. Voilå! Your virtual environment is activated and you are dropped into the newapp project directory, ready to get to work!
Find Your Projects
This is one of the best parts! I donât always remember what I called a project - I try to make the names mean something but I also like them to be short & sweet. So sometimes itâs a mystery to me what I might have called a project. With virtualenvwrapper
, mystery solved!
Create Just a virtualenv
Sometimes you want a different file structure, or want the project to live somewhere other than the default folder. You can still create a virtualenv and use workon myvenv
to activate it.
Other Useful Commands
If you have several projects that are related (so you want to use a different project file structure from virtualenvwrapperâs default), and they have different virtualenvs, you can create the virtualenvs with mkvirtualenv
. Then, when you have the project file structure the way you like it, you can use setvirtualenvproject
from each of the project folders to associate them to the virtualenv. Then when you use workon
to activate a virtualenv, it will put you into the correct project folder.
Further Readings
Here is a clickable link to the docs: virtualenvwrapper
Caveats
Thanks for reading!
I hope this has helped you understand virtualenvwrapper
! I have found it a very useful tool and for me it has been great. I like to go through lots of tutorials and this is a good way to keep them separated and keep track of them all.