Skip to content

Instantly share code, notes, and snippets.

@reillysiemens
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reillysiemens/9145563 to your computer and use it in GitHub Desktop.
Save reillysiemens/9145563 to your computer and use it in GitHub Desktop.
A brief introduction to using virtualenv and pip with Python 3.

###Using Virtualenv

To create and use a new Python 3 virtual environment you can run the commands below.

####Creating a Virtualenv

virtualenv -p /usr/bin/python3 ~/.virtualenvs/<name of your virtualenv>

####Sourcing a Virtualenv

To use your new virtualenv you'll need to source the virtualenv's activate script. Before sourcing this script your prompt will probably look something like user@hostname:~$. Source the script by running the following:

source ~/.virtualenvs/<name of your virtualenv>/bin/activate

Your prompt should now have changed to be something like (<name of your virtualenv>)user@hostname:~$. When you are finished using the virtualenv you'll need to deactivate it by running

deactivate

####Removing a Virtualenv

To remove a virtualenv you need only run

rm -rf ~/.virtualenvs/<name of your virtualenv>

####(Optional) Creating an Alias for a Virtualenv

Edit your .bashrc or .bash_aliases file to include a similar line.

alias <name of your alias>="source ~/.virtualenvs/<name of your virtualenv>/bin/activate

Exit and restart your shell or (preferably) source your modified .bashrc or .bash_aliases and you should now be able to run

<name of your alias>

anywhere and source your virtualenv without writing all that tedious junk above.

####More About Virtualenv

Need to know more about virtualenv? Run

virtualenv -h

to see virtualenv's help options and learn more about other commands.

###Using Pip

Most of the work that you do in a virtualenv will involve using pip. Your virtualenv should already have pip installed.

####Finding Pip Packages

Don't know what package(s) you want? Browse pip's package index.

####Installing Packages with Pip

To install a new package (for example, bottle) just run

pip install <name of package>

####Listing Currently Installed Pip Packages

To see what packages have been installed to your current virtualenv run

pip freeze

####Using a Requirements File in Pip

Your project may have certain dependencies that can be installed to your virtualenv's Python through pip. These dependencies are, by convention, listed in a file called requirements.txt. To install these dependencies to a new virtualenv run

pip install -r requirements.txt

####Updating Project Requirements

Have you installed pip packages that your project needs that are not found in the requirements file? Don't edit that requirements.txt file manually! Instead, to overwrite the current contents of the requirements.txt file, you can just run

pip freeze > requirements.txt

Now when you commit the new version of requirements.txt other developers can simply install your changes directly from the file.

####Uninstalling Pip Packages

To uninstall a pip package that you no longer want or need just run

pip uninstall <name of package>

####More About Pip

Need to know more about pip? Run

pip -h

to see pip's help options and learn more about other commands.

@bethanylong
Copy link

My devs and I like virtualenvwrapper for simplifying virtualenv tasks:

longb4@nari:~$ mkvirtualenv example
New python executable in example/bin/python
Installing distribute........................................................done.
Installing pip...............done.
virtualenvwrapper.user_scripts creating /home/longb4/.virtualenvs/example/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/longb4/.virtualenvs/example/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/longb4/.virtualenvs/example/bin/preactivate
virtualenvwrapper.user_scripts creating /home/longb4/.virtualenvs/example/bin/postactivate
virtualenvwrapper.user_scripts creating /home/longb4/.virtualenvs/example/bin/get_env_details
(example)longb4@nari:~$ deactivate
longb4@nari:~$ workon example
(example)longb4@nari:~$

@reillysiemens
Copy link
Author

Excellent suggestion. If anyone's looking to create a Python 3 virtualenv using virtualenvwrapper it's worth knowing that you can pass normal virtualenv options through the mkvirtualenv command like so:

mkvirtualenv -p /usr/bin/python3 ENVNAME

@Kellel
Copy link

Kellel commented Jul 10, 2014

you might need to run the following before you can use virtualenvwrapper

source /etc/bash_completion.d/virtualenvwrapper

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