4

So i am using vscode-remote in visual studio code insiders but i think the problem i am facing with vscode-python extension.

So i am trying to debug a python file . So the normal execution of the file is as follows :

i enable or activate a virtual environment with the following command :

source $HOME/.enn/enn.sh

but the path to the virtual environment as i can see in the shell file is : /home/ubuntu/venv/enn and pythonPath is /home/ubuntu/venv/enn/bin/python

So its a shell file which activates the relevant virtual environment and sets environment variables like export DOMAIN_ENDPOINT=http://s3.us-west-2.amazonaws.com etc.

So then i cd into the relevant directory and execute python command :

python session_server.py --config_file=../path/to/file/test_config.hocon

And this works .

But when i try to debug it starts giving import errors . So i think in debug mode environment variables are not set nor is the python virtual environment is activated. How to i set up the python virtual environment to be set up and environment variables also set up . So what i am essentially asking is how to i execute that same shell command - source $HOME/.enn/enn.sh during debugging with vscode

my launch.json is

{

    "version": "0.2.0",
    "configurations": [

        {
            "name": "Python: ennsession_server",
            "type": "python",
            "request": "launch",
            "program": "/home/ubuntu/path/to/file/session_server.py",
            "console": "integratedTerminal",
            "pythonPath" : "/home/ubuntu/venv/enn/bin/python",
            "cwd" : "/home/ubuntu/path/to/directory/experiment",
            "args": ["--config_file=../path/to/file/test_config.hocon"]


        }
    ]
} 

And also as i specified a pythonPath in settings.json for vscode as /home/ubuntu/venv/enn/bin/python so an automatic command is called by vscode when debugging as source /home/ubuntu/venv/enn/bin/activate. I dont want this to be called but i want the command source $HOME/.enn/enn.sh to be called when debugging or opening a new terminal. How do i do that?

user rk
  • 376
  • 5
  • 13

1 Answers1

1

There currently isn't a way to execute a shell script under the Python debugger because the debugger itself needs to start the Python code itself. If the shell script is activating the virtual environment and environment variables then you could let the extension do that on your behalf when debugging.

You can vote for this feature request to get the functionality you desire.

Brett Cannon
  • 14,438
  • 3
  • 45
  • 40
  • 1
    i have two questions : 1) how do i let the extension do the activating of virtual environment and set the environment variables – user rk Jun 14 '19 at 07:24
  • 2) And also as i specified a pythonPath in settings.json for vscode as "/home/ubuntu/venv/enn/bin/python" so an automatic command is called by vscode when debugging as "source /home/ubuntu/venv/enn/bin/activate". I dont want this to be called but i want the command "source $HOME/.enn/enn.sh" to be called when debugging or opening a new terminal. How do i do that? – user rk Jun 14 '19 at 07:24
  • 1 more question 3) how do i stop the the debugger to not automatically call "source /home/ubuntu/venv/enn/bin/activate" but use the environment i set up with the shell command in the integrated terminal. If the debugger uses the virtual environment i set up with the shell script in the terminal & not call the command "source /home/ubuntu/venv/enn/bin/activate"(which i think is called because i have set pythonPath in settings.json but i only set it up for pylint to work in editor but it affects terminal also ) on its own on each new terminal creation then also i think it should work . – user rk Jun 14 '19 at 07:31
  • 1) Activation is automatic unless you turn it off. – Brett Cannon Jun 14 '19 at 20:02
  • 1b) The [docs on debugging](https://code.visualstudio.com/docs/python/debugging) outline how to specify environment variables, as do the [docs for environment variable definition files](https://code.visualstudio.com/docs/python/environments#_environment-variable-definitions-file). 2) There isn't a way. 3) You can [turn of automatic virtual environment activation](https://code.visualstudio.com/docs/python/environments#_environments-and-terminal-windows) through a setting. – Brett Cannon Jun 14 '19 at 20:04