###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.
My devs and I like virtualenvwrapper for simplifying virtualenv tasks: