Tim*_*Mak 6 pip python-venv pipenv
据我所知,使用 pipelinev install 的标准方法是在虚拟环境之外安装。然而,这似乎与虚拟环境pip install
内部的旧做法相悖。
有没有办法在虚拟环境(venv)中安装软件包pipenv
?
如果我pip install
在 venv 内怎么办?除了不注册安装之外Pipfile
,还有什么区别?
会pipenv
知道我是否pip install
从外部进入 venv 内部?
非常感谢您的帮助。
\n\n有没有办法使用 pipelinev 在虚拟环境(venv)中安装软件包?
\n
是的。有一种情况是这样的:你有一个Pipfile
从旧的虚拟环境生成的,如下所示:
[[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 软件包。
您可以使用 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
软件包:
# 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\n如果我在 venv 中进行 pip 安装会怎样?除了不在 Pipfile 中注册安装之外,还有什么区别?
\n
如果你有并且venv
如果你这样做,这将以传统方式在 venv 中pip install <package>
安装包及其依赖项(如果它们在requirements.txt中,如果没有)。you have bad luck == you have to install them manually
pip 不考虑dependency conflicts
.
这就是pipenv
它进入游戏的原因。\nit 旨在管理依赖冲突并通过包安装所有必需的依赖项
(非常重要)如果您确实使用pip
该软件包进行安装,并且其依赖项将不会出现在 中Pipfile
,则安装的所有内容pipenv
都会出现在Pipfile
和 中Pipfile.lock
。
pipfile
和之间的差异pipfile.lock
pyproject.toml
. 它包含一个列表,其中包含用户(您,程序员)安装的所有软件包。这意味着包的依赖项(例如请求)将not
出现在 中pipfile
,它们将出现在 中pipfile.lock
。例如certifi
它的依赖项requests
,它不会出现在pipfile
,但是pipfile.lock
。pipfile 还包含您获取包的来源,它们可能是locally
、 frompypi
或 fromgithub
json
列表,包含您在项目 venv 中获得的所有内容这个问题的结论:pipenv 更好、更容易使用和管理项目的部门。推荐。你可以做任何你想做的事,朋友。
\n\n\n如果我从外部将 pip 安装在 venv 内部,pipenv 会知道吗?
\n
回答:No
。你必须手动管理它。
假设 venv 被激活并创建,并且安装了pipenv
类似的包而不是=>,那么情况将是这样的:pytest
pip install
pipenv install
在终端中快速测试
\npipenv install --dev\n
Run Code Online (Sandbox Code Playgroud)\n现在,让我们安装一个软件包pipenv install
,例如requests
\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\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)\npipfile
如果您(不小心)使用安装了软件包,那么这就是您可以更新的方法pip install
结论是pipenv
从头到尾都要使用。它将管理一切。
就是这样。现在您可以开始编码了。
\n 归档时间: |
|
查看次数: |
4823 次 |
最近记录: |