-
Notifications
You must be signed in to change notification settings - Fork 767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better handle imports in scripts #253
Comments
My specific case is as follows. I'm doing a course. It's organized by weeks, each week covering a given data structure or algorithm. Each week contains from 2 to 6 problems with a folder for each one. What I typically do is open a vscode in the week's folder and work on each problem. Sometimes I need to borrow code from a previous problem, that's why it is so handy to have the root on the week I'm working on. For each problem I create a test file (for pytest) and import the problem's code intro it to test it. |
A possible dumb suggestion: what about being able to add the This may be not a perfect solution but would allow some people to work with this issue, setting this in the global / workspace / folder settings to allow and disallow it in the places it needed. For example, enabling this in specific folders and workspaces, while keeping the rest as is. |
@ewerybody That would be covered by #275. env files are a detail of the Python extension, and has no idea how to process this format (let alone the Python extension's template-syntax config items...). |
I'm still having this error, so I filed a question on StackOverflow. Thanks in advance for your consideration. |
Perhaps some heuristics on when to decide whether or not a file is a script could be easily implemented?
I am aware that the FAQ recommends appending the script's folder to the extraPaths variable or using multi-root environments, but I think this is detrimental in a few ways:
Since Python is often picked up by beginners I'm afraid the current import behavior is an issue for newcomers. |
can you try setting this hidden option, in setting.json, "python.analysis.useImportHeuristic": true, and see whether it helps to resolve local imports? and whether it causes something to break? or whether it behaves differently than what you would expect? |
Almost complete beginner here (4 months of learning python; 2 months of experience with VSCode). I repeatedly had these warnings: |
This is curious, given the heuristic is supposed to be effectively resolving imports via the directory each file lives in. Is this repo available somewhere?
See #973 (and the reason why that's not simple in microsoft/pyright#1684 (comment)). |
@jakebailey Sorry, I typoed a lowercase The heuristic works fine and correctly detects imports in the same directory as the script. |
I'm going to close this in favor of #68; we're going to be able to close that issue soon, as the |
Invocations of scripts (
python path/to/script.py
) treat import paths differently than running code as modules. Specifically, invoking a script adds that script's directory to the front ofsys.path
, which means that its import resolution is different. This is problematic, as there's no way to know if any given file is supposed to be run as a script or not.This issue tracks better supporting this condition. Splitting this off of #52 (comment). See also microsoft/python-language-server#1602.
Perhaps there's some configuration that can be done in
pyrightconfig
/mspythonconfig
to mark certain files as scripts.The text was updated successfully, but these errors were encountered: