Pipenv 在虚拟环境中安装

Tim*_*Mak 6 pip python-venv pipenv

据我所知,使用 pipelinev install 的标准方法是在虚拟环境之外安装。然而,这似乎与虚拟环境pip install 内部的旧做法相悖。

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

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

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

非常感谢您的帮助。

ale*_*der 5

问题1

\n
\n

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

\n
\n

是的。有一种情况是这样的:你有一个Pipfile从旧的虚拟环境生成的,如下所示:

\n
[[source]]\nurl = "https://pypi.org/simple"\nverify_ssl = true\nname = "pypi"\n\n[packages]\nrequests = "*"\nrich = "*"\npylint = "*"\nbs4 = "*"\ndatedelta = "*"\ngtts = "*"\nkeyboard = "*"\npyperclip = "*"\npytz = "*"\npyttsx3 = "*"\npydub = "*"\nspeechrecognition = "*"\nscipy = "*"\npyowm = "*"\nimageio = "*"\nopencv-python = "*"\npyautogui = "*"\nmoviepy = "*"\nselenium = "*"\npyppeteer = "*"\nplaywright = "*"\npdfplumber = "*"\npypdf2 = "*"\ntqdm = "*"\nciphey = "*"\npytesseract = "*"\npyzbar = "*"\n\n[dev-packages]\npytest = "*"\n\n[requires]\npython_version = "3.10"\n
Run Code Online (Sandbox Code Playgroud)\n

您拥有该项目和Pipfile,但您在项目的根目录下创建了一个新的虚拟环境,并且dont have还安装了任何 python 软件包。

\n

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

\n
[[source]]\nurl = "https://pypi.org/simple"\nverify_ssl = true\nname = "pypi"\n\n[packages]\nrequests = "*"\nrich = "*"\npylint = "*"\nbs4 = "*"\ndatedelta = "*"\ngtts = "*"\nkeyboard = "*"\npyperclip = "*"\npytz = "*"\npyttsx3 = "*"\npydub = "*"\nspeechrecognition = "*"\nscipy = "*"\npyowm = "*"\nimageio = "*"\nopencv-python = "*"\npyautogui = "*"\nmoviepy = "*"\nselenium = "*"\npyppeteer = "*"\nplaywright = "*"\npdfplumber = "*"\npypdf2 = "*"\ntqdm = "*"\nciphey = "*"\npytesseract = "*"\npyzbar = "*"\n\n[dev-packages]\npytest = "*"\n\n[requires]\npython_version = "3.10"\n
Run Code Online (Sandbox Code Playgroud)\n

这将仅安装生产包。

\n

还安装所有软件包+dev软件包:

\n
# assuming in are in the project root\n# and the venv is activated\npipenv install\n
Run Code Online (Sandbox Code Playgroud)\n

这将安装all来自Pipfile

\n

问题2

\n
\n

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

\n
\n

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

\n

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

\n

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

\n

pipfile和之间的差异pipfile.lock

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

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

\n

问题编号 tris (3)

\n
\n

如果我从外部将 pip 安装在 venv 内部,pipenv 会知道吗?

\n
\n

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

\n

假设 venv 被激活并创建,并且安装了pipenv类似的包而不是=>,那么情况将是这样的:pytestpip installpipenv install

\n

在终端中快速测试

\n
pipenv install --dev\n
Run Code Online (Sandbox Code Playgroud)\n

现在,让我们安装一个软件包pipenv install,例如requests

\n
\n# NOTE\n# this is what happens if you install in an empty project\n# pipenv creates a new venv and creates pipfile and pipfile.lock\n\n\xef\x84\x94 [~/Alexzander__/programming/dev/python3/test]\n\xe2\x9d\xb1  pipenv install\nCreating a virtualenv for this project...\nPipfile: ~/Alexzander__/programming/dev/python3/test/Pipfile\nUsing /usr/bin/python (3.10.1) to create virtualenv...\n\xe2\xa0\xbc Creating virtual environment...created virtual environment \n\nbla bla\n\n\xe2\x9c\x94 Successfully created virtual environment!\n\nInstalling dependencies from Pipfile.lock (e4eef2)...\n     \xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89 0/0 \xe2\x80\x94 00:00:00\nTo activate this project virtualenv, run pipenv shell.\nAlternatively, run a command inside the virtualenv with pipenv run.\n\n\n\xef\x84\x94 [~/Alexzander__/programming/dev/python3/test]\n\xe2\x9d\xb1  ls\n# the great 2 files\n\xef\x80\x96 Pipfile  \xef\x80\xa3 Pipfile.lock\n\n\n\xef\x84\x94 [~/Alexzander__/programming/dev/python3/test]\n# activating venv\n\xe2\x9d\xb1  pipenv shell\nLaunching subshell in virtual environment...\n . ~/.local/share/virtualenvs/test-dG72Jjk6/bin/activate\n\n\n\xef\x84\x94 [~/Alexzander__/programming/dev/python3/test]\n\xe2\x9d\xb1   . ~/.local/share/virtualenvs/test-dG72Jjk6/bin/activate\n\n\xee\x98\x86 (test-dG72Jjk6)\n\xef\x84\x94 [~/Alexzander__/programming/dev/python3/test]\n\xe2\x9d\xb1\n# now the venv is active, you can see this `test-dG72Jjk6`\n\n\xee\x98\x86 (test-dG72Jjk6)\n\xef\x84\x94 [~/Alexzander__/programming/dev/python3/test]\n\xe2\x9d\xb1  which pip\n# pip is from current venv\n~/.local/share/virtualenvs/test-dG72Jjk6/bin/pip\n\n\xee\x98\x86 (test-dG72Jjk6)\n\xef\x84\x94 [~/Alexzander__/programming/dev/python3/test]\n# installing pytest using pip instead of pipenv\n\xe2\x9d\xb1  pip install pytest\nCollecting pytest ...\n\nbla bla\n\nSuccessfully 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\n\n# see ? pytest and its dependencies. good\n\n\n\xee\x98\x86 (test-dG72Jjk6)\n\xef\x84\x94 [~/Alexzander__/programming/dev/python3/test]\n\xe2\x9d\xb1  cat Pipfile\n\n# as you can see the `Pipfile` its empty\n\n[[source]]\nurl = "https://pypi.org/simple"\nverify_ssl = true\nname = "pypi"\n\n[packages]\n# pytest should appear here  <--------------------- ####\n\n[dev-packages]\n\n[requires]\npython_version = "3.10"\n\n# and also pipfile.lock its empty\n\xe2\x9d\xb1  cat Pipfile.lock\n{\n    "_meta": {\n        "hash": {\n            "sha256": "fedbd2ab7afd84cf16f128af0619749267b62277b4cb6989ef16d4bef6e4eef2"\n        },\n        "pipfile-spec": 6,\n        "requires": {\n            "python_version": "3.10"\n        },\n        "sources": [\n            {\n                "name": "pypi",\n                "url": "https://pypi.org/simple",\n                "verify_ssl": true\n            }\n        ]\n    },\n    "default": {},\n    "develop": {}\n}\n
Run Code Online (Sandbox Code Playgroud)\n

正如你现在所看到的,requests它在这里,也在 pipfile.lock 中(我也无法打印它,答案太长),但pytest它不在这里,也不会在这里。

\n

直到我这样做:

\n
\xe2\x9d\xb1  pipenv install requests\nInstalling requests...\nAdding requests to Pipfile [packages]...\n\xe2\x9c\x94 Installation Succeeded\nPipfile.lock (e4eef2) out of date, updating to (a290a1)...\nLocking [dev-packages] dependencies...\nLocking [packages] dependencies...\nBuilding requirements...\nResolving dependencies...\n\xe2\x9c\x94 Success!\nUpdated Pipfile.lock (a290a1)!\nInstalling dependencies from Pipfile.lock (a290a1)...\n     \xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89 0/0 \xe2\x80\x94 00:00:00\n\n\xee\x98\x86 (test-dG72Jjk6)\n\xef\x84\x94 [~/Alexzander__/programming/dev/python3/test]\n\xe2\x9d\xb1  cat Pipfile\n[[source]]\nurl = "https://pypi.org/simple"\nverify_ssl = true\nname = "pypi"\n\n[packages]\nrequests = "*" # <---------------------------- ###\n\n[dev-packages]\n\n[requires]\npython_version = "3.10"\n
Run Code Online (Sandbox Code Playgroud)\n

pipfile如果您(不小心)使用安装了软件包,那么这就是您可以更新的方法pip install

\n

结论是pipenv从头到尾都要使用。它将管理一切。

\n

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

\n