如何在虚拟环境中使用请求模块?

发布于 2025-02-03 14:31:49 字数 2878 浏览 2 评论 0原文

我目前正在为我的投资组合做一个项目,最近面临着我无法克服的障碍。我正在为我的项目使用虚拟的envoronment。我在该虚拟环境中安装了请求模块,并且在尝试导入该模块时,我得到了以下例外:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/requests/__init__.py", line 43, in <module>
    import urllib3
  File "/usr/lib/python3/dist-packages/urllib3/__init__.py", line 8, in <module>
    from .connectionpool import (
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 29, in <module>
    from .connection import (
  File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 39, in <module>
    from .util.ssl_ import (
  File "/usr/lib/python3/dist-packages/urllib3/util/__init__.py", line 3, in <module>
    from .connection import is_connection_dropped
  File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 3, in <module>
    from .wait import wait_for_read
  File "/usr/lib/python3/dist-packages/urllib3/util/wait.py", line 1, in <module>
    from .selectors import (
  File "/usr/lib/python3/dist-packages/urllib3/util/selectors.py", line 14, in <module>
    from collections import namedtuple, Mapping
ImportError: cannot import name 'Mapping' from 'collections' (/usr/lib/python3.10/collections/__init__.py)

我发现该例外的原因是urllib3的旧版本模块是从collections模块导入映射对象,尽管映射当前在collections.abc模块中。但这并不能解决我的问题,因为在我的虚拟环境中,我有更新的urllib3模块。

$ pip list
Package            Version
------------------ -----------
asgiref            3.5.2
certifi            2022.5.18.1
charset-normalizer 2.0.12
decouple           0.0.7
Django             4.0.4
idna               3.3
pip                22.1.1
requests           2.27.1
setuptools         58.1.0
sqlparse           0.4.2
urllib3            1.26.9

Python以某种方式使用了全局软件包,而不是在虚拟环境中使用包装。试图找到这种行为的原因,我发现当虚拟环境处于活动状态时,属性sys.prefixsys.exec_prefix应指向虚拟的基本目录环境。但是在我的情况下,这些属性指向/usr

$ python3.10
Python 3.10.4 (main, Apr  9 2022, 21:27:52) [GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.prefix
'/usr'
>>> sys.exec_prefix
'/usr'

我不明白为什么这是可能的,我该如何克服这一点。如果有人经历了这种行为,请给我一个暗示我应该走的方向。

编辑:

我开始Python的步骤:

python3.10 -m venv venv/
source venv/bin/activate
pip install requests
python3.10


(venv) mikhail@HP:~/Projects/Github/python/socialize$ which python
/home/mikhail/Projects/Github/python/socialize/venv/bin/python
(venv) mikhail@HP:~/Projects/Github/python/socialize$ which python3.10
/home/mikhail/Projects/Github/python/socialize/venv/bin/python3.10
(venv) mikhail@HP:~/Projects/Github/python/socialize$ 

I'm currently working on a project for my portfolio and recently faced an obstacle that I can't overcome. I'm using virtual envoronment for my project. I installed requests module within that virtual environment and while trying to import that module I got the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/requests/__init__.py", line 43, in <module>
    import urllib3
  File "/usr/lib/python3/dist-packages/urllib3/__init__.py", line 8, in <module>
    from .connectionpool import (
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 29, in <module>
    from .connection import (
  File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 39, in <module>
    from .util.ssl_ import (
  File "/usr/lib/python3/dist-packages/urllib3/util/__init__.py", line 3, in <module>
    from .connection import is_connection_dropped
  File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 3, in <module>
    from .wait import wait_for_read
  File "/usr/lib/python3/dist-packages/urllib3/util/wait.py", line 1, in <module>
    from .selectors import (
  File "/usr/lib/python3/dist-packages/urllib3/util/selectors.py", line 14, in <module>
    from collections import namedtuple, Mapping
ImportError: cannot import name 'Mapping' from 'collections' (/usr/lib/python3.10/collections/__init__.py)

I found out that the cause of this exception is that old version of urllib3 module is importing the Mapping object from collections module although the Mapping object currently is in collections.abc module. But that doesn't solve my problem because in my virtual enronment I have the updated urllib3 module.

$ pip list
Package            Version
------------------ -----------
asgiref            3.5.2
certifi            2022.5.18.1
charset-normalizer 2.0.12
decouple           0.0.7
Django             4.0.4
idna               3.3
pip                22.1.1
requests           2.27.1
setuptools         58.1.0
sqlparse           0.4.2
urllib3            1.26.9

Somehow python is using global packages and not packages from within virtual environment. Trying to find the cause of this behaviour I found out that when a virtual environment is active the attributes sys.prefix and sys.exec_prefix should point to the base directory of the virtual environment. But in my case these attributes point to /usr.

$ python3.10
Python 3.10.4 (main, Apr  9 2022, 21:27:52) [GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.prefix
'/usr'
>>> sys.exec_prefix
'/usr'

I can't understand why is that possible and how can I overcome this. If anyone have experienced this behaviour please give me a hint in what direction I should go.

Edited:

My steps to start python:

python3.10 -m venv venv/
source venv/bin/activate
pip install requests
python3.10


(venv) mikhail@HP:~/Projects/Github/python/socialize$ which python
/home/mikhail/Projects/Github/python/socialize/venv/bin/python
(venv) mikhail@HP:~/Projects/Github/python/socialize$ which python3.10
/home/mikhail/Projects/Github/python/socialize/venv/bin/python3.10
(venv) mikhail@HP:~/Projects/Github/python/socialize$ 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧故 2025-02-10 14:31:49

感谢@OneCricketeer将我指向正确的方向。我的环境有问题。我有多个Python版本,其中包含大量包装。我认为虚拟环境只会神奇地解决这个问题。但事实并非如此。

我的解决方案是删除系统(2.7,3.6)不需要的所有版本的Python,并使用Python版本管理系统 pyenv 虚拟环境插件。它易于使用,使我无法进一步混乱系统。

Thanks to @OneCricketeer for pointing me in the right direction. There was a problem with my environment. I had multiple python versions with a huge amount of packages. I thought virtual environment would just magically solve that problem. But it was not the case.

My solution is to remove all versions of python that are not required by the system (2.7, 3.6) and use Python version management system pyenv with its virtual environment plugin. It's easy to use and keeps me from cluttering up my system any further.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文