Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use pip install python packages locally like npm does

Say I have a project named Foo, and and want to install requests package locally for this project. What I am expecting is some structure similar to this:

Foo/
|-main.py
|-requirements.txt
|-README.md
|-python_modules/
|-|-requests
...

And I can do this by pip install -r requirments.txt -t ./python_modules/, however, this doesn't work properly because there are no __init__.py under python_modules/ so programs won't automatically import every packages in python_modules.

On the other hand, npm install does this very well.

So my question is, how to let pip work the same as npm does?

PS: I know there are other conventions using virtualenv or pythonbrew, but I still want to ask this question.

like image 380
mingyc Avatar asked May 04 '13 15:05

mingyc


Video Answer


1 Answers

The usual solution to this problem in the Python world is to use virtualenvs or, even better, a wrapper like pipenv. If you install pipenv, you should be able to create a new virtualenv with a simple pipenv install:

[user@host Foo]$ pipenv install
Creating a virtualenv for this
project… ⠋Using base prefix '/usr' New python executable in
/home/user/.local/share/virtualenvs/Foo-oXnKEj-P/bin/python3 Also
creating executable in
/home/user/.local/share/virtualenvs/Foo-oXnKEj-P/bin/python Installing
setuptools, pip, wheel...done.

Virtualenv location: /home/user/.local/share/virtualenvs/Foo-oXnKEj-P
Creating a Pipfile for this project… Pipfile.lock not found, creating…
Locking [dev-packages] dependencies… Locking [packages] dependencies…
Updated Pipfile.lock (c23e27)!  Installing dependencies from
Pipfile.lock (c23e27)… �� ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0 —
00:00:00 To activate this project's virtualenv, run the following: $
pipenv shell

Then enter the virtualenv with pipenv shell

[user@host Foo]$ pipenv shell
Spawning environment shell
(/bin/bash). Use 'exit' to leave. source
/home/user/.local/share/virtualenvs/Foo-oXnKEj-P/bin/activate

[user@host Foo]$ source /home/user/.local/share/virtualenvs/Foo-oXnKEj-P/bin/activate

Finally, you can install the packages in your requirements.txt:

(Foo-oXnKEj-P) [user@host Foo]$ pip install -r requirements.txt
Collecting Django==2.0.4 (from -r requirements.txt (line 1)) Using
cached
https://files.pythonhosted.org/packages/89/f9/94c20658f0cdecc2b6607811e2c0bb042408a51f589e5ad0cb0eac3236a1/Django-2.0.4-py3-none-any.whl
Collecting pytz (from Django==2.0.4->-r requirements.txt (line 1))
Using cached
https://files.pythonhosted.org/packages/dc/83/15f7833b70d3e067ca91467ca245bae0f6fe56ddc7451aa0dc5606b120f2/pytz-2018.4-py2.py3-none-any.whl
Installing collected packages: pytz, Django Successfully installed
Django-2.0.4 pytz-2018.4

(Foo-oXnKEj-P) [user@host Foo]$
like image 54
C. Tindall Avatar answered Sep 17 '22 20:09

C. Tindall