VS Code portable on Linux is still using for packages local user folder instead of the enviroment folder, and because of that imports fail

Viewed 183

I created a virtual environment with venv.

venv --python=/workspace/dev/python3.9 /var_path/var_virtual_env

In Visual Studio Code(portable version -Linux) I have set:

"python.venvPath": "/var_path/var_virtual_env",
"python.formatting.provider": "black"

The environment is seen in VSCode:

enter image description here

enter image description here

Code with python built-ins works without a problem.

From VS Code terminal with virtual environment activated using pip I installed different packages and I expected the packages to be imported in the virtual environment, but instead seems that they are installed in the/home/user_name/local/lib/python3.6.

Also I noticed that the configurations likepylint.d etc are at same location in "local". The pylint was set thru VSCode.

To fix it I installed the packages in the environment outside VSCode, but the editor doesn't sees them. Example httpx:

enter code here

But they are in the environment, pip freeze output:

enter image description here

How can I fix this ?

3 Answers

In a terminal where the scarlette environment is activated, copy the output of which python.

Set in settings.json the following line:

{
    "python.pythonPath": "paste_the_output_here"
    ....
}

I don't know if it will work in your case but that did the trick for me several times.

Click on:

enter image description here

And then choose python from venv.

Let know what happened.

I think virtual env is ok, but you didn't activated it in console.

And also I think it will be more intuitive if you create venv relatively, in the same folder as app, then just add it to .gitignore and .dockerignore

Execute this command to create virtual environment:

python -m venv .venv --prompt ${PWD##*/}

Then activate:

source .venv/bin/activate

More advanced:

echo "creating virtual environment"
python -m venv .venv --prompt ${PWD##*/}

echo "upgrading pip & wheel"
.venv/bin/python -m pip install -U pip
.venv/bin/pip install wheel

echo "installing dependencies from requirements.txt"
.venv/bin/pip install -r requirements.txt

Then activate:

source .venv/bin/activate

(You can also activate by opening new terminal instance in vscode, or aby destroying current one and opening new)

Related