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

Python Debugger is overiding PYTHONPATH #2509

Closed
tkuraku opened this issue Sep 6, 2018 · 18 comments · Fixed by #2554
Closed

Python Debugger is overiding PYTHONPATH #2509

tkuraku opened this issue Sep 6, 2018 · 18 comments · Fixed by #2554
Assignees
Labels
area-debugging bug Issue identified by VS Code Team member as probable bug important Issue identified as high-priority

Comments

@tkuraku
Copy link

tkuraku commented Sep 6, 2018

Environment data

  • VS Code version: 1.27.1
  • Extension version (available under the Extensions sidebar): 2018.8.0
  • OS and version: Windows 7
  • Python version (& distribution if applicable, e.g. Anaconda): Anaconda 5.2

Actual behavior

When I debug a python file in vscode my PYTHONPATH variable is overridden.

here is the relevant line shown on the terminal.

set "PYTHONPATH=c:\Users\username.vscode\extensions\ms-python.python-2018.8.0\pythonFiles\experimental\ptvsd"

Expected behavior

I would expect the PYTHONPATH to be appended to not overridden.

Steps to reproduce:

  1. Set the PYTHONPATH variable to something
  2. debug a script with the follow code:
import sys
print(sys.path)

You should not see the path that you set in the PYTHONPATH

@brettcannon brettcannon changed the title Python Debugger is setting the PYTHONPATH and overidding it. Python Debugger is overriging PYTHONPATH Sep 6, 2018
@brettcannon brettcannon added feature-request Request for new features or functionality area-debugging needs decision labels Sep 6, 2018
@brettcannon
Copy link
Member

I can't remember if we supported this in the old debugger (if we did then this is a regression). Otherwise I think of any reason off the top of my head to not append PYTHONPATH as appropriate.

@tkuraku
Copy link
Author

tkuraku commented Sep 6, 2018

Thank you for the quick reply. Hopefully this can be resolved.
If someone's python script/program relies on the PYTHONPATH being set then the current behavior of the debugger makes it unusable.

@tkuraku tkuraku changed the title Python Debugger is overriging PYTHONPATH Python Debugger is overidding PYTHONPATH Sep 6, 2018
@DonJayamanne
Copy link

@tkuraku
Please could you confirm you have a value in PYTHONPATH?

@tkuraku
Copy link
Author

tkuraku commented Sep 7, 2018

@DonJayamanne
I do have a value in PYHTONPATH. The reason I noticed this was that when I run my script from the terminal everything works as expected. However, when I ran the debugger my script failed with an import error on one of my modules that is in the PYTHONPATH. (FYI debugger works fine in pycharm).

@Argysh
Copy link

Argysh commented Sep 7, 2018

I'm having the same issue and it used to work a couple of days ago.
(vscode was updated to 1.26.1 in between.)

running my script in either ipython or python in the terminal works as expected. when running the debugger with these settings

        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}"
        },

shows that the PYTHONPATH is being overwritten to
"PYTHONPATH=/home/myname/.vscode/extensions/ms-python.python-2018.8.0/pythonFiles/experimental/ptvsd"

@Argysh
Copy link

Argysh commented Sep 7, 2018

adding individual softlinks to /home/myname/.vscode/extensions/ms-python.python-2018.8.0/pythonFiles/experimental/ptvsd to the required modules works for me as a temporary work-around

ln - s /home/path/to/my/module /home/myname/.vscode/extensions/ms-python.python-2018.8.0/pythonFiles/experimental/ptvsd/

@DonJayamanne
Copy link

@Argysh @tkuraku
Is PYTHONPATH defined in your python environment or is it a global environment variable?

@DonJayamanne DonJayamanne self-assigned this Sep 7, 2018
@DonJayamanne DonJayamanne added P2 bug Issue identified by VS Code Team member as probable bug and removed needs decision feature-request Request for new features or functionality labels Sep 7, 2018
@DonJayamanne
Copy link

DonJayamanne commented Sep 7, 2018

I believe the solution is not to invoke PTVSD as a package, but to run a python file that'll launch PTVSD with the path setup (e.g. sys.path.append.... or similar).

@brettcannon changing labels as this is a break from previous (old debugger) behavior.

@DonJayamanne DonJayamanne added important Issue identified as high-priority and removed P2 labels Sep 7, 2018
@tkuraku
Copy link
Author

tkuraku commented Sep 7, 2018

My PYTHONPATH is a global environment variable. I think that once the script has started that anything added with sys.path.append is fine.

@DonJayamanne
Copy link

@brettcannon
We are appending PTVSD to the exiting PYTHONPATH. However it looks like we're not getting the value inside the extension process. Could be related to #2363, #1403, and others

@brettcannon
Copy link
Member

@DonJayamanne are we running a separate process to get PYTHONPATH instead of just querying the environment that Node has in the extension itself?

@DonJayamanne
Copy link

are we running a separate process to get PYTHONPATH instead of just querying the

No we aren't running a separate process.
We just query the environment that node has in the extension.

@Argysh
Copy link

Argysh commented Sep 10, 2018

My PYTHONPATH is a global environment variable as well (set in .bashrc)

@dmitriyrepin
Copy link

dmitriyrepin commented Sep 11, 2018

After the recent upgrade, I have noticed the same issue on Linux (CentOS 7, Python 3.4, VS Code Version 1.12.1)

  1. $PYTHONPATH is defined as a global (bash) environment variable before starting a VS Code from the command line.

  2. VS Code picks it up. PYTHONPATH value can be observed in the VS Code terminal window

sh-4.2$ echo $PYTHONPATH
/host/viz-json-service/src/code:/host/viz-json-service/src/generated:/host/viz-json-service/src/tests
  1. Here is my launch.json:
        {
            "name": "Python: run_tests.py",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/src/run_tests.py",
            "console": "integratedTerminal",
        }
  1. However, when I start debugger (F5), the PYTHONPATH is overwritten:
sh-4.2$ cd /host/viz-json-service ; env "PYTHONIOENCODING=UTF-8" "PYTHONUNBUFFERED=1" "PYTHONPATH=/headless/.vscode/extensions/ms-python.python-2018.8.0/pythonFiles/experimental/ptvsd" /usr/bin/python3 -m ptvsd --host localhost --port 46405 /host/viz-json-service/src/main.py
  1. As a result, a package fails to load
Exception has occurred: ImportError
No module named 'code.viz_json'; 'code' is not a package
  File "/host/viz-json-service/src/tests/viz_json/test_util.py", line 6, in <module>

The same code run from a command line works fine:

$ python3 src/run_tests.py 
test_py_zgy (test_py_zgy.TestUtils) ... ok
  1. the code structure is as following:
root/
     code\
         __init__.py
        viz_json\
             __init__.py
             util.py
    tests\
       ....

--
...

@brettcannon
Copy link
Member

@dmitriyrepin FYI that the terminal in VS Code is a fresh terminal and shell (i.e. a separate process and such), so the fact the terminal picks up PYTHONPATH does not necessarily correlate that VS Code itself picked it up.

@dmitriyrepin
Copy link

FYI: I re-installed VS Code 1.26.1 + plugins in my container - same issue.
However, after installing VS Code 1.24.1 everything works as expected (so, the issue is a regression - it used to work with the old debugger):
sh-4.2$ cd /host/viz-json-service ; env "PYTHONIOENCODING=UTF-8" "PYTHONUNBUFFERED=1" /usr/bin/python3 /headless/.vscode/extensions/ms-python.python-2018.6.0/pythonFiles/PythonTools/visualstudio_py_launcher.py /host/viz-json-service 46428 34806ad9-833a-4524-8cd6-18ca4aa74f14 RedirectOutput,RedirectOutput /host/viz-json-service/src/run_tests.py

@Hareis
Copy link

Hareis commented Sep 12, 2018

Yes, I try to roll back to before version, i can start debugging code

@ericsnowcurrently
Copy link
Member

I was able to verify that my PYTHONPATH env var shows up in sys.path in a debugged program.

@brettcannon brettcannon changed the title Python Debugger is overidding PYTHONPATH Python Debugger is overiding PYTHONPATH Sep 19, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Oct 17, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-debugging bug Issue identified by VS Code Team member as probable bug important Issue identified as high-priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants