【问题标题】:pipenv install inside virtual environmentpipenv 在虚拟环境中安装
【发布时间】:2021-06-03 20:38:13
【问题描述】:

我了解使用 pipenv install 的标准方法是在虚拟环境之外安装 。但是,这似乎与pip installinside虚拟环境的旧做法相反。

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

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

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

非常感谢您的帮助。

【问题讨论】:

  • 我想你可能弄错了。 Pipenv 在虚拟环境“内部”安装包。如果没有虚拟环境处于活动状态,它将创建一个。 “pip install”的旧方法需要您激活环境,而“pipenv”则不需要。除此之外,我认为两者之间没有区别,而不是在 Pipfile 中注册。 Here is a helpful article
  • 感谢这篇有用的文章。我确实了解 pipenv 安装在 venv 的“内部”。但是,如果您在 venv 内运行 pipenv,它将在 venv 内创建另一个 venv,这不是我想要的,所以只需询问 pipenv 是否有另一种语法可以在 venv 内正确执行它。我想答案是“不”。
  • 如果你有一个激活的虚拟环境,那么pipenv实际上并没有在另一个环境中安装包,它就像pip一样正常安装。 Refer here.
  • 是的,你是对的。

标签: pip pipenv python-venv


【解决方案1】:

问题编号 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,但是你在项目的根目录下创建了一个新的虚拟环境并且你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

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

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

这就是为什么pipenv 进入游戏的原因。 它旨在管理依赖冲突并通过包安装所有必需的deps

(非常重要)如果您确实使用pip 安装该软件包并且其依赖项不会出现在Pipfile 中,那么使用pipenv 安装的所有内容都将出现在PipfilePipfile.lock 中。

pipfilepipfile.lock 之间的差异

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

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

问题编号 (3)

pipenv 会知道我是否从外部安装在 venv 中吗?

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

假设使用pipenv 激活并创建了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 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。它会管理一切。

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

【讨论】:

    猜你喜欢
    • 2019-10-26
    • 1970-01-01
    • 1970-01-01
    • 2019-05-16
    • 2014-02-26
    • 2021-07-14
    • 2020-10-12
    • 2020-06-03
    • 2017-05-14
    相关资源
    最近更新 更多