Setting up your python environment

If you’ve worked with python but never used virtualenv, then you will probably have encountered this problem. You are working on 2 projects. One requires version X of a library; the other requires version Y of the same library.

So you use virtualenv to create isolated python environments so you don’t have messy and conflicting dev environments in your system.

But here’s another tool that’s a bit easier to use. It’s a wrapper for virtualenv called virtualenvwrapper.

Here’s the basics.

Download it

pip install virtualenvwrapper

Now add these 2 lines to your ~/.bash_profile (if you’re on OSX)

export WORKON_HOME=~/virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

(Note, the 2nd line may be different depending on where virtualenvwrapper was installed)

Create a virtual env

mkvirtualenv env1

Exit from the virtual env

deactivate

Next time you want to work on that project again

workon env1

If you forgot what virtual envs you have created, you can list them

lsvirtualenv

Bonus Material: Attaching PyCharm to your VirtualEnv
If you use PyCharm as your python IDE, then you should know that you can attach your project to these virtualenv.

Open Preferences then search for “interpreter” or look under Project > Project Interpreter.
Then click on the gear icon in the top right and select “Add…”
Screen Shot 2019-02-15 at 6.00.48 PM

Then select “Existing environment” and click “…” to find the virtualenv’s python executable you created using virtualenvwrapper above.
Screen Shot 2019-02-15 at 6.02.12 PM
If you set your WORKON_HOME to ~/virtualenvs as instructed above, then you’ll find your virtualenv under ~/virtualenvs.
And if your virtual env is called env1, then you’ll select ~/virtualenvs/env1/bin/python (or ~/virtualenvs/env1/bin/python3 if you want to use python3)

Advertisements
Tagged , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: