virtualenv and pip - a Python environment in 60 seconds
I'm a PHP developer completely new to Python. For this project, I want to make sure that everyone's using the same environment, which means virtualenv and pip. As an added bonus, this means that your pip installs are per project, and not system wide. Here's a quick guide to getting it all set up on Ubuntu.
First, install pip
bashsudo apt-get install python-pip
Then install virtualenv
bashsudo pip install virtualenv
Next, create your project directory and cd into it
bashmkdir new-project && cd new-project
You'll want to create a virtualenv environment for this new project (using the python2.7 interpreter)
bashvirtualenv -p /usr/bin/python2.7 --no-site-packages .virtualenv
The next thing to do is to enable it
bashsource .virtualenv/bin/activate
And install any dependencies
bashpip install twisted
Once you've installed your dependencies, you'll want to record them somewhere. By convention, pip uses requirements.txt. To generate this file from your currently installed modules, use the following command:
bashpip freeze > requirements.txt
To install the dependencies from the file, use the following command:
bashpip install -r requirements.txt
Finally, you'll want to share this project via git or a similar tool. Make sure that your .virtualenv folder is in your ignore file. Your coworkers will be able to rebuild the environment easily.
bashecho ".virtualenv" > .gitignoregit add .git commit -m "Initial commit"
When a coworker downloads the project, all they need to do to get up and running is run the following:
bashvirtualenv -p /usr/bin/python2.7 .virtualenv && source pyenv/bin/activate && pip install -r requirements.txt