JamesK
JamesK

Reputation: 49

Understanding pip, virtualenv and packages

I'm a bit confused with what is happening, but I may be just misunderstanding how virtualenv is meant to work.

First, I discovered I was getting errors because the path to my git folder had spaces in it. After removing spaces from the path, I created a fresh virtualenv, and then when activated pip list started working properly - showing what was installed into the site-packages dir. Note, I did not create the venv with --no-site-packages, and I did not create a requirements.txt with pip freeze.

Here's where the confusion starts...

At home, I git pull to sync up, and I see the new venv folder, but:

  1. Activating the venv and using pip list does not show the packages that were installed at work/into the repo.
  2. Example, the PyQt folder is less than half the size it was at work. Note QT itself was installed at work but not at home (standalone installation obviously, not pip). Another example is openpyxl. Folder is there, but not mentioned in pip list.

Does pip freeze exist because getting things setup on a separate computer means you need to globally install what is listed in requirements.txt (if I had created one)? I thought the venv would contain everything and packages don't need to be installed since they are already in the folder.

I know its mentioned in virtualenv docs to gitignore the env, but I don't see why. And I've heard its easier to have it in the repo. Unless of course this is a no-no, hence my troubles.

I would appreciate some guidance understanding how pip, venv, and git are best used together for using multiple computers (and of course multiple people). You would think Googling would solve it, but so far these specifics have eluded me.

Thanks

Upvotes: 0

Views: 172

Answers (2)

A H Bensiali
A H Bensiali

Reputation: 875

First of all, Im not an expert on setting this up but here goes.

Git is version controlling. You take a snap shot of files & the changes of files with git. Like setting up a checkpoint, its important to monitor & track was is done & changed with version controls.

Virtualenv is merely a virtual environment. Like setting up a server, you are setting up a virtual environment of Python.

With virtualenv you can set which version of python/pip is used. pip is a package management system. It helps you pull the package you require & all its dependencies. If a library requires other items, then it will be pulled as well. This type of 'library management' is available in a lot of languages.

With virtualenv, you can specify what you want to use; python2.7 or 3.5, or whatever version you need, provided it is on the machine it is being utilized.

after you active your virtualenv, you can pip install the libraries you need. Of course, these libraries are specific to that when you activate that virtualenv in that project.

requirement.txt file is just a place to take a snapshot of what you have "pip install"ed. You can transfer that file to another project and install those libraries else where. Alternatively, you can use git to put those libraries in a repo but you may have issues.

Personally, I dont think I can work without a virtualenv. One of my projects may require python2.7, another I saw required python3.4 so it allows you the flexibility to it.

Also when setting up your server (apache or nginx) you need to point to what virtualenv you need your site to run with.

Pertaining to all this setting up, Im a mere novice but love python & django.

I hope I have done this justice. Happy coding

Please see the following docs for more granular configurations & explanations.

[https://virtualenv.pypa.io/en/stable/][1]

[https://github.com/pypa/pip][1]

[https://git-scm.com/book/en/v2/Getting-Started-Git-Basics][1]

Upvotes: 0

Simon Bilsky-Rollins
Simon Bilsky-Rollins

Reputation: 545

In my experience the best way to manage Python projects across multiple computers is NOT to try to distribute pip packages or virtualenv installations along with your program because that can lead to all sorts of problems. In fact, I'm not even sure that what you're trying to do is possible. Instead I would recommend the following:

  1. Exclude your virtualenv installation from your git repo by adding env to your .gitignore file.

  2. Run pip freeze > requirements.txt to write all required packages to requirements.txt.

  3. On any other computers you need to run the program on, run pip install -r requirements.txt to install the required packages.

This approach, besides being quite straightforward, also gives you (and anyone else who may want to run your program) the flexibility to set up their local Python environment however they want to.

Upvotes: 1

Related Questions