Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explain once and for all how to set python.pythonPath using environment variables or remove this from docs #10533

Closed
smac89 opened this issue Mar 12, 2020 · 5 comments
Labels
area-environments Features relating to handling interpreter environments bug Issue identified by VS Code Team member as probable bug

Comments

@smac89
Copy link

smac89 commented Mar 12, 2020

Environment data

  • VS Code version: 1.44.0
  • Extension version (available under the Extensions sidebar): 2020.2.64397
  • OS and version: Linux x64 5.5.8-arch1-1
  • Python version: Virtual env python 2.7
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): Pipenv
  • Jedi or Language Server?: disabled
  • Value of the python.languageServer setting: Microsoft

Expected behaviour

From reading the docs, it seems to suggest that one can set python.pythonPath using environment variable.

I have a .env file with the content:

PYTHON_INSTALL_LOC="/home/chigozirim/dev/virtualenvs/XXX/bin/python"

And I have set the value of python.pythonPath using:

"python.pythonPath": "${env:PYTHON_INSTALL_LOC}"

I expect the plugin to pick up the actual path from the environment variable and use that python

Actual behaviour

The plugin fails to see the python I specified and instead picks up the system default python, which means the variable is resolving to an empty string

Steps to reproduce:

Use my setup above, with the .env file.

Directory structure

➜ tree -aL 1
.
├── data
├── .env
├── fig
├── .git
├── .gitignore
├── mnist
├── Pipfile
├── Pipfile.lock
├── .pylintrc
├── README.md
├── run.ps1
├── src
└── .vscode

Logs

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

[Info  - 6:41:44 p.m.] Analysis cache path: /home/chigozirim/.cache/Microsoft/Python Language Server
[Info  - 6:41:44 p.m.] Microsoft Python Language Server version 0.5.31.0
[Info  - 6:41:44 p.m.] Workspace root: /home/chigozirim/projects/neural-networks-and-deep-learning-py
[Info  - 6:41:44 p.m.] GetCurrentSearchPaths /usr/bin/python 
[Info  - 6:41:44 p.m.] Interpreter search paths:
[Info  - 6:41:44 p.m.]     /usr/lib/python3.8
[Info  - 6:41:44 p.m.]     /usr/lib/python3.8/lib-dynload
[Info  - 6:41:44 p.m.]     /home/chigozirim/.local/lib/python3.8/site-packages
[Info  - 6:41:44 p.m.]     /usr/lib/python3.8/site-packages
[Info  - 6:41:44 p.m.] User search paths:
[Info  - 6:41:44 p.m.]     /home/chigozirim/projects/neural-networks-and-deep-learning-py/src
[Info  - 6:41:47 p.m.] Initializing for /usr/bin/python
[Info  - 6:41:47 p.m.] Analysis caching mode: None.

In case I'm not making myself clear, I am referring to where it states in the docs:

By default, the Python extension looks for and loads a file named .env in the current workspace folder, then applies those definitions. The file is identified by the default entry "python.envFile": "${workspaceFolder}/.env" in your user settings (see General settings). You can change the python.envFile setting at any time to use a different definitions file.

I'm certain this means that the extension is supposed to read the .env file and use the values found there. If that's the case, why does it fail to use the environment variables I set, to fill in python.pythonPath?

I just need to know if this feature is supported or not and what the docs means when it specifies the use of environment variables for setting the python.pythonPath. If this is actually not supported, then I strongly suggest it should be, otherwise it should not be in the docs so that devs are not being misled and end up wasting time on something that wasn't supposed to work.

See also:
#944 (comment)

@smac89 smac89 added triage-needed Needs assignment to the proper sub-team bug Issue identified by VS Code Team member as probable bug labels Mar 12, 2020
@ghost ghost removed the triage-needed Needs assignment to the proper sub-team label Mar 12, 2020
@ericsnowcurrently
Copy link
Member

@smac89, thanks for getting in touch with us about this. We'll get back to you as soon as possible.

@ghost
Copy link

ghost commented Mar 22, 2020

I think I'm facing the same issue.
I want to add sub-folders to the PYTHONPATH, to import python files in the same folder.
I'm using .env file and the Pylint and python-debugger recognize the .env file, but when I try to run the file in python run Current file in python interactive window or run python file in terminal the python throw ModuleNotFoundError

@ericsnowcurrently
Copy link
Member

@smac89, I was able to verify the behavior as described: any setting using an environment variable defined only in a .env file is effectively ignored. Note that this has always been the case. Also note that env var substitution works as documented for regular environment variables (not in .env file).

The cause is that the extension code which applies environment variables in settings does not currently use those defined in .env files. In fact, .env files are currently only used when the extension creates subprocesses or new terminals.

The resolution of the problem you've encountered is one of the following:

  1. apply the .env file when doing variable substitution
  2. update the docs to clarify that env var substitution in settings ignores .env files
  3. do nothing

We'll look into which makes the most sense.

@ericsnowcurrently ericsnowcurrently removed their assignment Mar 24, 2020
@ericsnowcurrently ericsnowcurrently added area-environments Features relating to handling interpreter environments needs decision and removed triage labels Mar 24, 2020
@smac89
Copy link
Author

smac89 commented Mar 24, 2020

Thanks @ericsnowcurrently
Hopefully decision number 1 is taken, because it will prove to be the most useful seeing as support for .env is already popular among virtualenv enthusiasts and already has complete support by pipenv. It is also easy to add support, as you just need to source .env in a bash-like environment, or simply load it with python-dotenv or environs

@luabud
Copy link
Member

luabud commented Apr 10, 2020

Thanks for bringing this up! We'll update the docs accordingly. FWIW, we're also deprecating the python.pythonPath setting (#2125).

Closing in favour of microsoft/vscode-docs#3550 and #11015.

@luabud luabud closed this as completed Apr 10, 2020
@ghost ghost removed the needs decision label Apr 10, 2020
@lock lock bot locked as resolved and limited conversation to collaborators Apr 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-environments Features relating to handling interpreter environments bug Issue identified by VS Code Team member as probable bug
Projects
None yet
Development

No branches or pull requests

4 participants