问题编号 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 安装的所有内容都将出现在Pipfile 和Pipfile.lock 中。
pipfile 和 pipfile.lock 之间的差异
- pipfile 就像
pyproject.toml。它包含一个列表,其中包含用户(您,程序员)安装的所有软件包。这意味着包的依赖项,例如请求,将出现在not 中,它们将出现在pipfile.lock 中。例如certifi它是requests的依赖,它不会出现在pipfile,而是pipfile.lock。 pipfile 还包含您获取软件包的来源,它们可以是locally、pypi 或github
- 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。它会管理一切。
就是这样。现在你可以开始编码了。