Search code examples
pythonvisual-studio-codevscode-debuggervscode-remotevscode-python

how to execute a shell script which sets up the python virtual environment when debugging with vscode ( What changes do i need to make to launch.json)


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?


Solution

  • 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.