vscode-code-runner
vscode-code-runner copied to clipboard
Add support for PYTHONPATH
Currently, PYTHONPATH
is not supported for python so user can not import package inside the project root folder
I am not familiar with PYTHONPATH
. What is it used for?
Actually it is just a environment variable. It is used for Python to find path of their modules. If this is not defined correctly, import
in Python will raise error since it can not find path of project's modules.
Maybe more universe solution is just to respect env
in launch.json
where user can define "PYTHONPATH": "${workspaceRoot}"
in env
@jxltom have a look at the implementation pushed in 0.9.2. https://github.com/formulahendry/vscode-code-runner/releases/tag/0.9.2
Happy to iterate if you feel something was missed.
@bodak this is a great addition to your extension, thank you!
Would you consider making $pythonPath the default when launching python files (if it is defined/the python extension is available)? Your extension is very popular and so we have had a number of users of the Python extension who are using the code runner extension and are confused as to why their selected interpreter is not being used when running code.
E.g. the following post on StackOverflow caused quite a bit of confusion, only after a screencast did we realize the code runner extension was involved: https://stackoverflow.com/questions/50110691/unable-to-select-python-interpreter
CC @donjayamanne @brettcannon
Hi @qubitron , thank you! Based on the popularity of the Python extension, it makes sense to make $pythonPath as default to run Python. I have created a separate issue #301 to track this, will implement this.
@formulahendry I can have a look at this tomorrow.
We can probably close this issue (#238) now.
There seems to be some confusion here, between PYTHONPATH and the path to the interpreter/executable.
To be clear, @qubitron is not referring to the environment variable PYTHONPATH
, instead he (we) are referring to the path to the python interpreter (executable). So, if you have the python extension installed, this path (to executable/interpreter) would be retrieved from the setting python.pythonPath
in settings.json
.
In the python extension the value for PYTHONPATH
is defined either within an environments file (e.g. .env
or a file defined in settings.json
, see here)
@jxltom have a look at the implementation pushed in 0.9.2. https://github.com/formulahendry/vscode-code-runner/releases/tag/0.9.2
Happy to iterate if you feel something was missed.
How can one specify the "custom parameter"? I have not found it in the list of possible settings: am I looking in the wrong place?
@gennaro-tedesco Please refer to https://github.com/formulahendry/vscode-code-runner#configuration
The path to be specified in the configuration is the path to the Python interpreter on your local machine. The PYTHONPATH instead is the path, in the current working directory, where Python must look for imports, libraries and the like.
Real example of python path is you create different A and B folders with python scripts in each, then you create __init__.py
file in each folder, which declares folder as package(module), so it means you can import package B at package A or/and viceversa. But to do that you will need to put first package name and then needed file ( in python you can't reference outer python script without tricks), so import became like:
from B.some_python_file import SomeClass
And to be able to run that script, people create special run.sh file with next content:
#!/bin/sh
export PYTHONPATH=src/folder_which_contains_a_and_b_folders && python3.7 $*
and you use it as:
sh bin/run.sh src/folder_which_contains_a_and_b_folders/A/my_script.py
so python now treats folder_which_contains_a_and_b_folders as root, and do import from it, and not from folder where you run script
@jxltom have a look at the implementation pushed in 0.9.2. https://github.com/formulahendry/vscode-code-runner/releases/tag/0.9.2
Happy to iterate if you feel something was missed.
Well, something is definitely missing. This is for setting the python interpreter, not the PYTHONPATH environment variable. Two very distinct things. PYTHONPATH is like your system's PATH, but for python, it works on the same logic.
I would like to have this feature. I spent a whole day to figure out what's wrong with my code when importing modules and it turns out the extension doesn't support PYTHONPATH and the code only works when executing it in terminal. It would be nice to run it directly in vscode. Thanks.
I'm still facing the same problem with Code Runner 0.11.6. My code runs fine on terminal as I have PYTHONPATH setup using .env file at the root folder of my python but it doens't work when I use Code Runner in the OUTPUT tab of VS CODE. Can any one please help me? I have wasted whole day today on this.
It's 2023, still no support for .env files as part of code-runner? Would absolutely love to have this feature.