Skip to content

Instantly share code, notes, and snippets.

@simonw
Created July 18, 2017 23:53
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save simonw/4835a22c79a8d3c29dd155c716b19e16 to your computer and use it in GitHub Desktop.
Save simonw/4835a22c79a8d3c29dd155c716b19e16 to your computer and use it in GitHub Desktop.
How to set up a Python virtual environment

How to set up a Python development environment

A Python development environment is a folder which you keep your code in, plus a "virtual environment" which lets you install additional library dependencies for that project without those polluting the rest of your laptop.

mkdir my-new-python-project
cd my-new-python-project
virtualenv --python=python2.7 venv
# This will create a my-new-python-project/venv folder
touch .gitignore
subl .gitignore
# Add venv to your .gitignore

Now any time you want to work on your project, you need to "activate your virtual environment". Do that like this:

cd my-new-python-project
source venv/bin/activate

You can tell it is activated because your terminal prompt will now look like this:

(venv) computer:my-new-python-project user$ 

Note the (venv) bit at the start.

Once it is activated, any time you run the "python" or "pip" commands it will be running the special custom versions in your venv folder.

You could also run them without activating the virtual environment like this:

venv/bin/python
venv/bin/pip

To install new python libraries, use pip like this:

pip install requests
pip install ipython

It's always a good idea to install ipython.

@simonw
Copy link
Author

simonw commented Sep 30, 2017

Here's how to sign the Python interpreter on OS X so it won't keep asking for network port permission: https://gist.github.com/simonw/68d19a46e8edc2cd8c68

@ram574
Copy link

ram574 commented Feb 7, 2022

is it good to push venv into github?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment