activated venv - import error even though package is installed in the venv

别来无恙 提交于 2021-02-08 06:48:51

问题


i'm working on python 3.7 with Linux Ubuntu 18.04 and i'm trying to import a requests from the venv that i've created.

What I have done is install python3.7 and set it to the default via vim ~/.bashrc. When I type python -V in the terminal, it now prints python 3.7.3.

I've also installed VirtualEnv with this tutorial and installed the venv to my project directory. Within the project directory, I can clearly see that the venv as well as the other folders such as bin, include, lib, share folders have also been created. (include folder has python3.7m added to it). (lib/python3.7/site-packages also have the initial packages such as pip, setuptools, etc...)

What I did next was to use terminal and activate venv with source venv/bin/activate. From there I did a pip install requests and I can see that it have been installed in venv/lib/python3.7/site-packages.

Problem

while venv is still activated:

(venv) tinker@tinkerboard:~/jngm/Project Space/Crde$ python
Python 3.7.3 (default, Jun  5 2019, 14:36:28) 
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'requests'
>>> 

However:

>>> import os
>>> os.chdir('venv/lib/python3.7/site-packages/')
>>> import requests
>>> a = requests.get(http://www.google.com')
>>>

What is happening that I cannot do a import requests directly?

*note: while venv is activated, i did a pip install requests and not a sudo pip install requests - as it was recommended by online tutorials.

Edit

which python inside venv: /home/tinker/jngm/Project Space/Crde/venv/bin/python

which python outside venv: /usr/bin/python

which python3.7 outside venv: /usr/local/bin/python3.7

The below code shows the sys.path while venv is activated

>>> import sys
>>> print(sys.path)
['', '/usr/local/lib/python37.zip', '/usr/local/lib/python3.7', '/usr/local/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/site-packages']
>>> 

# while `venv` is activated:
>>> echo $PATH
>>> /home/tinker/jngm/Project Space/Crde/venv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

来源:https://stackoverflow.com/questions/56457474/activated-venv-import-error-even-though-package-is-installed-in-the-venv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!