2

I want to use a .sh file as a wrapper for executing Python because I need to import specific environment variables before running my Python scripts. The structure of my .sh file (my_python.sh) looks like this:

SCRIPT_DIR=some_path
export PATH1=some_path_2
source another_script.sh

python_exe=${PYTHONEXE:-"${SCRIPT_DIR}/kit/python/bin/python3"}
$python_exe "$@" $args || error_exit

I can successfully run Python scripts by executing this .sh file directly in the terminal, e.g.:

./my_python.sh my_script.py

This works fine, as the necessary environment variables are sourced before script execution.

However, when I try to set this wrapper script my_python.sh as the Python interpreter in VS Code using "Enter interpreter path", VS Code instead selects the python_exe (the resolved path to python3) directly. This bypasses the .sh script, so the required environment variables aren't imported, and the execution of my_script.py fails.

Is there a way to configure VS Code to use the .sh script as if it was the Python interpreter so that it correctly sources the environment variables? I believe it is possible because my colleague could make it work, but we could not figure out the differences between our setups. My VS Code version is 1.95.3.

I tried reinstalling VS Code, changed the location of the .sh file. The only workaround I found was to define the environment variables in a launch.json but this solution does not seem to be scalable.

3
  • Instead of a custom shell script as the python interpreter, could you source the script with environment variables in your .bashrc or equivalent? Or do you need different environment variables for different projects? Commented Dec 2, 2024 at 13:24
  • Yes I could source those in my .bashrc but I would like to use the .sh script directly since this script is provided to me externally and the environment variables might change over time. I would like to be able to select this script as interpreter in order to use the debugger. Commented Dec 2, 2024 at 13:51
  • Could you use a virtual environment for this use-case? See VSCode docs - python - manually specify interpreter. I believe this is a VSCode core feature? I couldn't find a related open issue on the VSCode issue tracker maybe your scenario is a bug - or you're really supposed to only use the alternatives listed in the docs. Commented Dec 2, 2024 at 16:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.