This is applicable to both OS X and Linux. I use the same tools on both systems. In this guide, I'm going to set up an environment for a Flask app.
-
I start by installing pip, virtualenv, then virtualenvwrapper. I seldom use virtualenv directly; I use the wrapper tools instead.
-
I create a virtualenv for my project:
$ mkvirtualenv myflaskapp New python executable in myflaskapp/bin/python Installing setuptools............done. Installing pip...............done.
-
I then use
pip
to install any libraries that I need, e.g.:$ pip install flask $ pip install Flask-WTF
-
I then create a directory for my own python files (you can do this anywhere. I often just put this in my home directory).
$ mkdir -p myflaskapp
-
And if I'm just building a simple project, or just doing some exploratory programming, I'll just write all of my code in a single
main.py
file.$ cd myflaskapp && touch main.py
-
Do some work in that file!
Once I've stopped working on this project for a while, I'll come back to it, and reactivate my virtualenv:
workon myflaskapp
Now, If I need to install any other tools (like Flask-Mail), I can use pip to do so!
pip install Flask-Mail
And because I'm working in a project-specific virtual environment, my installed python libraries are separate from other projects.
If you want to distribute your app, or if you want to work on another system, it's nice to be able to recreate your virtualenv. You do this by:
- Freezing your requirements (with
pip freeze
) - Creating a new virtualenv and re-loading from a requirements file.
First, let's see what pip freeze
does:
$ cd myflaskapp
$ pip freeze
Flask==0.10.1
Flask-WTF==0.9.5
Jinja2==2.7.2
MarkupSafe==0.23
WTForms==1.0.5
Werkzeug==0.9.4
dulwich==0.9.5
hg-git==0.5.0
itsdangerous==0.24
numpy==1.7.1
wsgiref==0.1.2
It just lists all of our install libraries (and their dependencies). You can save this in a requirements.txt file:
$ pip freeze > requirements.txt
Now, on any other system, you can create a new virtual environment:
mkvirtualenv myflaskapp
Then, re-install all of your libraries from a requirements file.
$ pip install -r requirements.txt
Thats it!
You can also take a look at pip help
to see what else pip can do for you!
Good explanation. Where do you keep python, static etc. files? in virtualenv or outside of it? It's not clear for me from: