在 Python 世界中,这个问题的通常解决方案是使用 virtualenvs,或者更好的是,像 pipenv 这样的包装器。如果你安装了 pipenv,你应该能够用一个简单的方法创建一个新的 virtualenv 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
然后进入virtualenvpipenv 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
最后,您可以在 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]$