Vscode-code-runner: Add support for PYTHONPATH

Created on 14 Jan 2018  路  12Comments  路  Source: formulahendry/vscode-code-runner

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

feature request

Most helpful comment

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.

All 12 comments

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?

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ranolfi picture ranolfi  路  3Comments

w3adventures picture w3adventures  路  3Comments

Salil03 picture Salil03  路  5Comments

N2ITN picture N2ITN  路  5Comments

mjaniec2013 picture mjaniec2013  路  5Comments