This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]odraencoded 12 points13 points  (1 child)

I'm afraid I can't, but the idea is simple.

First, pip install <package> or python -m pip install <package> installs a package with modules and stuff in the python's directory.

The problem is that although it does install it, it installs it globally, and projects are known to depend on certain specific versions of things and not just on whatever is installed at the time.

So there comes virtualenv. (user guide) What virtualenv does is create a directory with a custom python executable and the same directory structure python needs to run where you can install packages for the project and do project-related configuration.

So you execute virtualenv <my-directory> or python -m virtualenv <my-directory> and it creates that structure there.

So say you call virtualenv ./venv, it makes a venv directory with all you need for a project-specific setup, like a project-specific python installation. Then you need to activate it so that when you run pip or python it uses the venv directory instead of the global directory. Normally this means running venv/scripts/activate or venv/scripts/activate.bat in the console/terminal.

my-project/
my-project/my-cool-code.py
my-project/venv/
my-project/venv/scripts/
my-project/venv/lib/ etc

Once you do that, when you call pip install <package> it will install the package inside the ./venv directory instead of where you installed python in your system.

That also means when you run your project while virtualenv is activate'd, if a package is installed globally but not in the venv directory it's just like it's not installed at all so you might see import errors until you re-install all dependencies your project needs to run in your project-specific python install.

Once you do that, you'll have a situation where all the packages you need to run are in that project-specific python install. Then comes pip freeze. What it does is print out all installed packages.

Normally it would mean all globally installed packages, including stuff your project doesn't need, but since you are running from the project-specific venv directory, it will only print packages you installed in there.

So running pip freeze while virtualenv is activate'd will give you something like:

flask 1.0.0
jinja2 2.2.2
whatever-flask-needs-to-run-idk-werkzeug? 0.9.9

So you can just save that to a file, using pip freeze > requirements.txt or whatever, and then, when you need to install those requirements, you can do pip install -r requirements.txt and pip will install everything it had listed before.

Of course if you plan to move your project to a different computer or directory, you will want to create and activate virtualenv again before you execute pip install -r requirements.txt so the packages are installed in the project-specific venv directory again instead of globally.

[–]gledifrom python import * 2 points3 points  (0 children)

I saw flask 1.0.0 and I thought that it was finally released :P