Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

Python Pip not installing package into the correct directory in virtual environment Visual Studio for Mac - "unresolved import"

EDIT: See WORKAROUND in reply - if you get "unresolved import" on a package you have installed this might be the reason.

Even though VScode settings are set to use the venv interpreter, and the virtual environment is activated, when using pip or pip3 to install a package in the VSCode terminal, packages are installed into the default python directory.

e.g.

(.venv) person@MacBook-Pro my-code-lives-here % pip3 install python-dotenv

results after trying it twice so you can see the install path:

Requirement already satisfied: python-dotenv in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (0.15.0)

VS code settings.json:

"python.pythonPath": ".venv/bin/python3",

Same command was working last week as I have other packages installed.

question from:https://stackoverflow.com/questions/66052734/python-pip-not-installing-package-into-the-correct-directory-in-virtual-environm

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

WORKAROUND

use pip's --target arg to specify the site-packages dir of the virutal environment:

e.g

pip3 install --target=/<path-to-code-folder>/.venv/lib/python3.8/site-packages python-dotenv


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...