虚拟环境中运行pipenv install

6

我知道使用pipenv install的标准方式是在虚拟环境外部进行安装。然而,这似乎与在虚拟环境中使用pip install的旧做法相矛盾。

  1. 是否有一种方法可以使用pipenv在虚拟环境(venv)中安装包?

  2. 如果我在venv内使用pip install,会发生什么?除了不在Pipfile中注册安装之外,还有什么区别吗?

  3. 如果我从外部在venv中使用pip install进行安装,pipenv会知道吗?

非常感谢您的帮助。


我认为你可能误解了。Pipenv会在虚拟环境中安装软件包。如果没有激活虚拟环境,它将创建一个虚拟环境。而旧的“pip install”方法需要您激活环境,而“pipenv”则不需要。除此之外,我认为两者之间没有什么区别,只是在Pipfile中注册。这里有一篇有用的文章 - AKD
1
感谢那篇有用的文章。我知道pipenv是在venv内安装的。但是,如果你在venv内运行pipenv,它会在venv内创建另一个venv,这不是我想要的,所以我想问一下pipenv是否有另一种语法可以在venv内正确地执行。我猜答案是“没有”。 - Tim Mak
2
如果您已经激活了虚拟环境,那么 pipenv 实际上不会在另一个环境中安装软件包,它的行为类似于 pip 并正常安装。请参考此处 - AKD
是的,你说得对。 - Tim Mak
确保你在pipenv的pipfile所在的正确目录中。我在我的~/.zshrc文件中有一些代码改变了目录,所以在执行pipenv install <package>之前,我需要先切换到正确的目录。然后它就可以正常工作了。 - wordsforthewise
1个回答

4

问题1

如何使用pipenv在虚拟环境(venv)中安装包?

可以这样做:你有一个旧的虚拟环境生成的 Pipfile,像这样:

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
requests = "*"
rich = "*"
pylint = "*"
bs4 = "*"
datedelta = "*"
gtts = "*"
keyboard = "*"
pyperclip = "*"
pytz = "*"
pyttsx3 = "*"
pydub = "*"
speechrecognition = "*"
scipy = "*"
pyowm = "*"
imageio = "*"
opencv-python = "*"
pyautogui = "*"
moviepy = "*"
selenium = "*"
pyppeteer = "*"
playwright = "*"
pdfplumber = "*"
pypdf2 = "*"
tqdm = "*"
ciphey = "*"
pytesseract = "*"
pyzbar = "*"

[dev-packages]
pytest = "*"

[requires]
python_version = "3.10"

您已经有项目和Pipfile文件,但是您在项目根目录下创建了一个新的虚拟环境,并且还没有安装任何Python包。

您可以使用pipenv从Pipfile安装依赖项:

# assuming in are in the project root
# and the venv is activated
pipenv install

这将仅安装生产包。
同时安装所有包 + dev 包:
pipenv install --dev

这将安装Pipfile中的所有软件包。
问题2:
如果您有一个venv并使用pip install ,则这将以传统方式在venv中安装软件包及其依赖项(如果它们在requirements.txt中,如果没有,你需要手动安装)。pip不考虑依赖冲突。这就是为什么pipenv出现的原因,它被设计用于管理依赖冲突并安装一个软件包所需的所有依赖项。
非常重要的一点是,如果您使用pip进行安装,则软件包及其依赖项将不会出现在Pipfile中。使用pipenv安装的所有内容都将显示在Pipfile和Pipfile.lock中。
Pipfile类似于pyproject.toml。它包含由用户(您,程序员)安装的所有软件包列表。这意味着软件包(例如requests)的依赖关系不会出现在pipfile中,而是会出现在pipfile.lock中。例如certifi是requests的一个依赖项,它不会出现在pipfile中,但会出现在pipfile.lock中。Pipfile还包含获取软件包的源代码的信息,它们可以是本地的,也可以是从pypi或github获得的。
Pipfile.lock包含软件包的元数据和一些哈希值。它还包含已安装的每个软件包和每个依赖项,每个生产软件包和每个开发软件包以及它们的哈希值。基本上,它是一个json列表,其中包含您项目venv中所有内容。
结论:pipenv更好,更容易使用和管理您的项目依赖项。它是推荐的。你可以随心所欲,朋友。
问题3:
Pipenv是否知道我从外部在venv中使用pip安装?
答案:不知道。您需要手动管理。
假设venv已启用并使用pip install而不是pipenv install安装了pytest软件包,则会出现以下情况:

# NOTE
# this is what happens if you install in an empty project
# pipenv creates a new venv and creates pipfile and pipfile.lock

 [~/Alexzander__/programming/dev/python3/test]
❱  pipenv install
Creating a virtualenv for this project...
Pipfile: ~/Alexzander__/programming/dev/python3/test/Pipfile
Using /usr/bin/python (3.10.1) to create virtualenv...
⠼ Creating virtual environment...created virtual environment 

bla bla

✔ Successfully created virtual environment!

Installing dependencies from Pipfile.lock (e4eef2)...
     ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0 — 00:00:00
To activate this project virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.


 [~/Alexzander__/programming/dev/python3/test]
❱  ls
# the great 2 files
 Pipfile   Pipfile.lock


 [~/Alexzander__/programming/dev/python3/test]
# activating venv
❱  pipenv shell
Launching subshell in virtual environment...
 . ~/.local/share/virtualenvs/test-dG72Jjk6/bin/activate


 [~/Alexzander__/programming/dev/python3/test]
❱   . ~/.local/share/virtualenvs/test-dG72Jjk6/bin/activate

 (test-dG72Jjk6)
 [~/Alexzander__/programming/dev/python3/test]
❱
# now the venv is active, you can see this `test-dG72Jjk6`

 (test-dG72Jjk6)
 [~/Alexzander__/programming/dev/python3/test]
❱  which pip
# pip is from current venv
~/.local/share/virtualenvs/test-dG72Jjk6/bin/pip

 (test-dG72Jjk6)
 [~/Alexzander__/programming/dev/python3/test]
# installing pytest using pip instead of pipenv
❱  pip install pytest
Collecting pytest ...

bla bla

Successfully installed attrs-21.4.0 iniconfig-1.1.1 packaging-21.3 pluggy-1.0.0 py-1.11.0 pyparsing-3.0.6 pytest-6.2.5 toml-0.10.2

# see ? pytest and its dependencies. good


 (test-dG72Jjk6)
 [~/Alexzander__/programming/dev/python3/test]
❱  cat Pipfile

# as you can see the `Pipfile` its empty

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
# pytest should appear here  <--------------------- ####

[dev-packages]

[requires]
python_version = "3.10"

# and also pipfile.lock its emptycat Pipfile.lock
{
    "_meta": {
        "hash": {
            "sha256": "fedbd2ab7afd84cf16f128af0619749267b62277b4cb6989ef16d4bef6e4eef2"
        },
        "pipfile-spec": 6,
        "requires": {
            "python_version": "3.10"
        },
        "sources": [
            {
                "name": "pypi",
                "url": "https://pypi.org/simple",
                "verify_ssl": true
            }
        ]
    },
    "default": {},
    "develop": {}
}

现在,让我们使用 pipenv install 安装一个包,比如 requests

❱  pipenv install requests
Installing requests...
Adding requests to Pipfile [packages]...
✔ Installation Succeeded
Pipfile.lock (e4eef2) out of date, updating to (a290a1)...
Locking [dev-packages] dependencies...
Locking [packages] dependencies...
Building requirements...
Resolving dependencies...
✔ Success!
Updated Pipfile.lock (a290a1)!
Installing dependencies from Pipfile.lock (a290a1)...
     ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0 — 00:00:00

 (test-dG72Jjk6)
 [~/Alexzander__/programming/dev/python3/test]
❱  cat Pipfile
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
requests = "*" # <---------------------------- ###

[dev-packages]

[requires]
python_version = "3.10"

正如你所见,现在 requests 已经存在,同时也在 pipfile.lock 文件中(我不能打印出来,答案太长了),但是 pytest 没有出现而且不会出现。

直到我执行以下操作:

❱  pip freeze
attrs==21.4.0
certifi==2021.10.8
charset-normalizer==2.0.10
idna==3.3
iniconfig==1.1.1
packaging==21.3
pluggy==1.0.0
py==1.11.0
pyparsing==3.0.6
pytest==6.2.5
requests==2.27.1
toml==0.10.2
urllib3==1.26.7

 (test-dG72Jjk6)
 [~/Alexzander__/programming/dev/python3/test]
❱  pip freeze > requirements.txt

# installing packages from requirements file
❱  pipenv install -r requirements.txt
Requirements file provided! Importing into Pipfile...
Pipfile.lock (a290a1) out of date, updating to (856742)...
Locking [dev-packages] dependencies...
Locking [packages] dependencies...
Building requirements...
Resolving dependencies...
✔ Success!
Updated Pipfile.lock (856742)!
Installing dependencies from Pipfile.lock (856742)...
     ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0 — 00:00:00


 (test-dG72Jjk6)
 [~/Alexzander__/programming/dev/python3/test]
❱  cat Pipfile
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
requests = "==2.27.1"
attrs = "==21.4.0"
certifi = "==2021.10.8"
charset-normalizer = "==2.0.10"
idna = "==3.3"
iniconfig = "==1.1.1"
pluggy = "==1.0.0"
py = "==1.11.0"
pyparsing = "==3.0.6"
pytest = "==6.2.5" # <----------------------------- ITS HERE :)
toml = "==0.10.2"
urllib3 = "==1.26.7"

[dev-packages]

[requires]
python_version = "3.10"

这就是如果您(不小心)使用pip install安装软件包后如何更新pipfile的方法。

结论是从一开始到结束都使用pipenv。它将管理所有内容。

就这些了,现在您已经准备好编码了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接