vscode-code-runner icon indicating copy to clipboard operation
vscode-code-runner copied to clipboard

Add support for PYTHONPATH

Open jxltom opened this issue 7 years ago • 16 comments

Currently, PYTHONPATH is not supported for python so user can not import package inside the project root folder

jxltom avatar Jan 14 '18 13:01 jxltom

I am not familiar with PYTHONPATH. What is it used for?

formulahendry avatar Jan 15 '18 02:01 formulahendry

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.

jxltom avatar Jan 15 '18 02:01 jxltom

Maybe more universe solution is just to respect env in launch.json where user can define "PYTHONPATH": "${workspaceRoot}" in env

jxltom avatar Jan 15 '18 02:01 jxltom

@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 avatar Mar 14 '18 12:03 bodak

@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

qubitron avatar May 03 '18 02:05 qubitron

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 avatar May 03 '18 03:05 formulahendry

@formulahendry I can have a look at this tomorrow.

We can probably close this issue (#238) now.

bodak avatar May 03 '18 09:05 bodak

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)

DonJayamanne avatar May 03 '18 16:05 DonJayamanne

@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 avatar Oct 12 '18 08:10 gennaro-tedesco

@gennaro-tedesco Please refer to https://github.com/formulahendry/vscode-code-runner#configuration

formulahendry avatar Nov 01 '18 06:11 formulahendry

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.

gennaro-tedesco avatar Nov 01 '18 21:11 gennaro-tedesco

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

Oleksandr-Silin avatar Nov 18 '18 16:11 Oleksandr-Silin

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

daniele-niero avatar Jul 09 '21 10:07 daniele-niero

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.

macsir avatar Jul 12 '21 07:07 macsir

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.

waqas-24 avatar Nov 12 '21 16:11 waqas-24

It's 2023, still no support for .env files as part of code-runner? Would absolutely love to have this feature.

sharpencrag avatar Feb 10 '23 03:02 sharpencrag