pipenv 在虚拟环境中安装

pipenv install inside virtual environment

我了解使用 pipenv install 的标准方法是在虚拟环境外部 安装。但是,这似乎违背了pip install inside虚拟环境的老做法

  1. 有没有办法用 pipenv 在虚拟环境 (venv) 中安装软件包?

  2. 如果我 pip install 在 venv 中怎么办?除了不在 Pipfile 中注册安装外,还有什么区别?

  3. pipenv 会知道我是否从外部pip install进入 venv 吗?

非常感谢您的帮助。

问题编号1

Is there a way to install packages inside the virtual environment (venv) with pipenv?

是的。有这样一种情况:你有一个 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,但是您在项目的根目录下创建了一个新的虚拟环境,并且 dont have 安装了所有 python 包。

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

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

这将只安装生产包。

同时安装所有软件包 + dev 个软件包:

pipenv install --dev

这将从 Pipfile

安装 all 个软件包

问题编号2

What if I pip install within the venv? Apart from not registering the install in Pipfile, what is the difference?

如果你有 venv 并且你有 pip install <package>,这将安装包及其依赖项(如果它们在 requirements.txt 中,如果不在 you have bad luck == you have to install them manually) 在 venv 中只是传统的方式。 pip 没有考虑 dependency conflicts.

这就是 pipenv 它进入游戏的原因。 它旨在管理依赖性冲突并通过软件包安装所有必需的依赖项

(非常重要)如果你使用 pip 安装,包及其依赖项将不会出现在 Pipfile 中,所有使用 pipenv 安装的都将出现在 [=16= 中] 和 Pipfile.lock.

pipfilepipfile.lock

之间的差异
  • pipfile 就像一个 pyproject.toml。它包含一个列表,其中包含用户(您,程序员)安装的所有软件包。这意味着包的依赖项,例如请求,将 not 出现在 pipfile 中,它们将出现在 pipfile.lock 中。例如 certifi 它是 requests 的依赖项,它不会出现在 pipfile 中,而是出现在 pipfile.lock 中。 pipfile 还包含您获取包的来源,它们可以是 locallypypigithub
  • pipfile.lock 包含有关包的元数据和一些哈希值。它还包含每个已安装的包和每个依赖项、每个生产包和每个开发包以及它们的哈希值。基本上它是一个 json 列表,其中包含您在项目 venv
  • 中获得的所有内容

这个问题的结论:pipenv 更好用,更容易使用和管理你的项目部门。它的推荐。你可以做任何你想做的事,朋友。

问题 numero tres (3)

Will pipenv know if I pip installed inside venv from outside?

答案:No。你必须手动管理它。

假设 venv 是用 pipenv 激活和创建的,并且像 pytest 这样的包是用 pip install 而不是 pipenv install 安装的 => 那么它就是这个情况:

终端中的快速测试


# 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 empty
❱  cat 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。它将管理一切。

就是这样。现在您可以开始编码了。