我知道使用pipenv install的标准方式是在虚拟环境外部进行安装。然而,这似乎与在虚拟环境中使用pip install的旧做法相矛盾。
是否有一种方法可以使用pipenv在虚拟环境(venv)中安装包?
如果我在venv内使用pip install,会发生什么?除了不在Pipfile中注册安装之外,还有什么区别吗?
如果我从外部在venv中使用pip install进行安装,pipenv会知道吗?
非常感谢您的帮助。
我知道使用pipenv install的标准方式是在虚拟环境外部进行安装。然而,这似乎与在虚拟环境中使用pip install的旧做法相矛盾。
是否有一种方法可以使用pipenv在虚拟环境(venv)中安装包?
如果我在venv内使用pip install,会发生什么?除了不在Pipfile中注册安装之外,还有什么区别吗?
如果我从外部在venv中使用pip install进行安装,pipenv会知道吗?
非常感谢您的帮助。
如何使用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
pipenv install --dev
# 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
。它将管理所有内容。
就这些了,现在您已经准备好编码了。
pipenv
,它会在venv内创建另一个venv,这不是我想要的,所以我想问一下pipenv是否有另一种语法可以在venv内正确地执行。我猜答案是“没有”。 - Tim Makpipenv
实际上不会在另一个环境中安装软件包,它的行为类似于pip
并正常安装。请参考此处。 - AKD