简体   繁体   中英

How can I use a requests module within virtual environment?

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$ 

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM