1

我在虚拟环境中运行 python3,但是当我尝试导入新安装的包时,我收到一条ImportError: No module named ...错误消息,尽管我在运行!env/bin/pip3 list.

当我键入时,sys.path我看不到任何指向我的 virtualenv 的路径:

['',
'/Library/Frameworks/Python.framework/Versions/3.5/lib/python35.zip',
'/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5',
'/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/plat-darwin',
'/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/lib-dynload',
'/Users/zurfarosa/Library/Python/3.5/lib/python/site-packages',
'/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages',
'/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/IPython/extensions',
'/Users/zurfarosa/.ipython',
'/Users/zurfarosa']

如何将我的虚拟环境添加到我的 PYTHONPATH?

4

1 回答 1

0

就我而言,我不得不更改解释器,因为默认值是全局的,而不是虚拟环境中的。

输出打印路径sys.paths(在哪里import寻找):

c:\Coding\py scripts\test_interpreter_location
C:\Users\[NAME]\AppData\Local\Programs\Python\Python37-32\python37.zip
C:\Users\[NAME]\AppData\Local\Programs\Python\Python37-32\DLLs
C:\Users\[NAME]\AppData\Local\Programs\Python\Python37-32\lib
C:\Users\[NAME]\AppData\Local\Programs\Python\Python37-32
C:\Users\[NAME]\AppData\Roaming\Python\Python37\site-packages
C:\Users\[NAME]\AppData\Local\Programs\Python\Python37-32\lib\site-packages
C:\Users\[NAME]\AppData\Local\Programs\Python\Python37-32\lib\site-packages\win32
C:\Users\[NAME]\AppData\Local\Programs\Python\Python37-32\lib\site-packages\win32\lib
C:\Users\[NAME]\AppData\Local\Programs\Python\Python37-32\lib\site-packages\Pythonwin

将 IDE 解释器更改为Python.exe内部虚拟环境目录后,现在给出:

c:\Coding\py scripts\test_interpreter_location
c:\users\[NAME]\appdata\local\programs\python\python37-32\python37.zip
c:\users\[NAME]\appdata\local\programs\python\python37-32\DLLs
c:\users\[NAME]\appdata\local\programs\python\python37-32\lib
c:\users\[NAME]\appdata\local\programs\python\python37-32
C:\Coding\py scripts\test_interpreter_location\env
C:\Coding\py scripts\test_interpreter_location\env\lib\site-packages # <- where your env installed packages are

要更改 VSCode 中的解释器,请单击右下角(屏幕截图中的 env 解释器处于活动状态): 在此处输入图像描述

于 2022-02-15T10:45:37.397 回答