首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python 3‘`venv`’和‘the’包

Python 3‘`venv`’和‘the’包
EN

Stack Overflow用户
提问于 2021-09-29 00:08:29
回答 1查看 1.7K关注 0票数 14

从python3.8开始,我注意到对于没有错误的wheel包,pip install包似乎是必需的(至少它们看起来像错误,但行为像警告一样)。

注意:在我的所有示例中,我都是通过禁用我的环境、删除环境和清除pip缓存来清理中间的:

代码语言:javascript
复制
deactivate
rm test_env/ -rf
rm ~/.cache/pip/ -rf

例1:问题

代码语言:javascript
复制
python3.8 -m venv test_env
source test_env/bin/activate
pip install markuppy

我得到以下输出:

代码语言:javascript
复制
Collecting markuppy
  Downloading MarkupPy-1.14.tar.gz (6.8 kB)
Building wheels for collected packages: markuppy
  Building wheel for markuppy (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: /home/k/test_env/bin/python3.8 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-tfm9bgxv/markuppy/setup.py'"'"'; __file__='"'"'/tmp/pip-install-tfm9bgxv/markuppy/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-cpx9gxcn
       cwd: /tmp/pip-install-tfm9bgxv/markuppy/
  Complete output (6 lines):
  usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
     or: setup.py --help [cmd1 cmd2 ...]
     or: setup.py --help-commands
     or: setup.py cmd --help
  
  error: invalid command 'bdist_wheel'
  ----------------------------------------
  ERROR: Failed building wheel for markuppy
  Running setup.py clean for markuppy
Failed to build markuppy
Installing collected packages: markuppy
    Running setup.py install for markuppy ... done
Successfully installed markuppy-1.14

它出错了,因为没有安装wheel包。关于这一问题的其他帖子也提到了这一点。

示例2:通过在venv中安装wheel来解决问题

如果我在创建新的venv之后直接安装wheel包,则不会发生这样的情况:

代码语言:javascript
复制
python3.8 -m venv test_env
source test_env/bin/activate
pip install wheel
pip install markuppy

输出:

代码语言:javascript
复制
Collecting markuppy
  Using cached MarkupPy-1.14.tar.gz (6.8 kB)
Building wheels for collected packages: markuppy
  Building wheel for markuppy (setup.py) ... done
  Created wheel for markuppy: filename=MarkupPy-1.14-py3-none-any.whl size=7413 sha256=52b3e5c3e317ae21724acd871fe3deb85dde9df305b20d16f2c5592c43b11e91
  Stored in directory: /home/k/.cache/pip/wheels/95/13/60/31c9d5f4cd012e491aeac154ef8b0ec964916523623eb02f0b
Successfully built markuppy
Installing collected packages: markuppy
Successfully installed markuppy-1.14

这也适用于给定一个requirements.txt文件。

requirements.txt

代码语言:javascript
复制
markuppy
代码语言:javascript
复制
python3.8 -m venv test_env
source test_env/bin/activate
pip install wheel
pip install -r requirements.txt

输出:

代码语言:javascript
复制
Collecting markuppy
  Using cached MarkupPy-1.14.tar.gz (6.8 kB)
Building wheels for collected packages: markuppy
  Building wheel for markuppy (setup.py) ... done
  Created wheel for markuppy: filename=MarkupPy-1.14-py3-none-any.whl size=7413 sha256=52b3e5c3e317ae21724acd871fe3deb85dde9df305b20d16f2c5592c43b11e91
  Stored in directory: /home/k/.cache/pip/wheels/95/13/60/31c9d5f4cd012e491aeac154ef8b0ec964916523623eb02f0b
Successfully built markuppy
Installing collected packages: markuppy
Successfully installed markuppy-1.14

示例3:将wheel包添加到requirements.txt无助

重要的是要知道,如果您将wheel包放置到requirements.txt文件中,并且不单独安装它,则会遇到与示例1相同的问题:

requirements.txt

代码语言:javascript
复制
wheel
markuppy
代码语言:javascript
复制
python3.8 -m venv test_env
source test_env/bin/activate
pip install -r requirements.txt

输出:

代码语言:javascript
复制
Collecting wheel
  Downloading wheel-0.37.0-py2.py3-none-any.whl (35 kB)
Collecting markuppy
  Downloading MarkupPy-1.14.tar.gz (6.8 kB)
Building wheels for collected packages: markuppy
  Building wheel for markuppy (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: /home/k/test_env/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-z7cqzaej/markuppy/setup.py'"'"'; __file__='"'"'/tmp/pip-install-z7cqzaej/markuppy/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-kw3_8ayt
       cwd: /tmp/pip-install-z7cqzaej/markuppy/
  Complete output (6 lines):
  usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
     or: setup.py --help [cmd1 cmd2 ...]
     or: setup.py --help-commands
     or: setup.py cmd --help
  
  error: invalid command 'bdist_wheel'
  ----------------------------------------
  ERROR: Failed building wheel for markuppy
  Running setup.py clean for markuppy
Failed to build markuppy
Installing collected packages: wheel, markuppy
    Running setup.py install for markuppy ... done
Successfully installed markuppy-1.14 wheel-0.37.0

车轮安装正常,但没有及时安装markuppy包。

示例4:在系统级别安装wheel无助于

如果我直接将wheel包安装到系统本身,那么所创建的venv就无法访问它,因此我得到了与示例1相同的结果。

代码语言:javascript
复制
sudo apt install python3-wheel
python3.8 -m venv test_env
source test_env/bin/activate
pip install markuppy

输出:

代码语言:javascript
复制
Collecting markuppy
  Downloading MarkupPy-1.14.tar.gz (6.8 kB)
Building wheels for collected packages: markuppy
  Building wheel for markuppy (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: /home/k/test_env/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-5xkssq1l/markuppy/setup.py'"'"'; __file__='"'"'/tmp/pip-install-5xkssq1l/markuppy/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-aunof5xf
       cwd: /tmp/pip-install-5xkssq1l/markuppy/
  Complete output (6 lines):
  usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
     or: setup.py --help [cmd1 cmd2 ...]
     or: setup.py --help-commands
     or: setup.py cmd --help
  
  error: invalid command 'bdist_wheel'
  ----------------------------------------
  ERROR: Failed building wheel for markuppy
  Running setup.py clean for markuppy
Failed to build markuppy
Installing collected packages: markuppy
    Running setup.py install for markuppy ... done
Successfully installed markuppy-1.14

示例5:在系统级别安装wheel并授予to对系统站点包的访问权限。

如果我在系统级别安装了wheel,那么在创建系统站点包时,授予我对系统站点包的访问权限(请注意venv命令中的标志),那么这个问题不会发生。

代码语言:javascript
复制
sudo apt install python3-wheel
python3.8 -m venv test_env --system-site-packages
source test_env/bin/activate
pip install markuppy

输出:

代码语言:javascript
复制
Collecting markuppy
  Downloading MarkupPy-1.14.tar.gz (6.8 kB)
Building wheels for collected packages: markuppy
  Building wheel for markuppy (setup.py) ... done
  Created wheel for markuppy: filename=MarkupPy-1.14-py3-none-any.whl size=7414 sha256=cefe8d9f20cecaf72253cab1e18acbdcb6d30827d7a3fd5a74bbef4935bc2e44
  Stored in directory: /home/k/.cache/pip/wheels/95/13/60/31c9d5f4cd012e491aeac154ef8b0ec964916523623eb02f0b
Successfully built markuppy
Installing collected packages: markuppy
Successfully installed markuppy-1.14

这种方法的明显缺点是,它打破了项目的venv与安装在系统本身上的python包之间的隔离。

例6:在Python3.7中,所有这些都没有发生

代码语言:javascript
复制
python3.7 -m venv test_env
source test_env/bin/activate
pip install -U pip
pip install markuppy

输出:

代码语言:javascript
复制
Collecting markuppy
  Downloading MarkupPy-1.14.tar.gz (6.8 kB)
Using legacy 'setup.py install' for markuppy, since package 'wheel' is not installed.
Installing collected packages: markuppy
    Running setup.py install for markuppy ... done
Successfully installed markuppy-1.14

尽管它确实提供了一个警告,即pip使用setup.py install而不是wheel

问题

我们该如何处理这个wheel包?似乎我们所有人都已经安装了这个软件包(全部?)环境,但是标准库中的环境工具不会自动提供它。

我喜欢将我的项目环境与系统python环境完全隔离的想法,因此我看到的唯一选项是:

在创建一个新的venv .

  • 之后,
  1. 会立即手动安装wheel包,除了使用venv之外的另一个工具来管理wheel
EN

回答 1

Stack Overflow用户

发布于 2022-08-10 15:57:32

在设置venv时,在运行其他任何内容之前,只需先运行pip install --upgrade pip wheel即可。无论如何,这是一个很好的实践,可以确保您使用最新版本的pip来解决依赖关系,因此警告如下:

代码语言:javascript
复制
WARNING: You are using pip version 22.0.4; however, version 22.2.2 is available.
You should consider upgrading via the '/home/user/tmp/test_env/bin/python3 -m pip install --upgrade pip' command.

在默认情况下,轮子似乎不再包括在内,因为我也可以在3.10上进行复制。如果指定--system-site-packages来使用系统轮,那么venv的全部目的就是避免接触到系统python。在requirements.txt安装期间不使用也是有意义的,因为不太可能有任何软件包将wheel作为依赖项列出,因此很可能是并行安装的。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69369669

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档