-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Python file with more than one entry breaks module resolution #3727
Comments
I tried to create a brand new project and recreate this. The custom module is resolved without issue. When I try and import modules from my initial project I still cannot resolve them. In both projects I get an error message in the workload console like:
|
Found the issue. Looks to be a corrupted .mypy_cache. I removed the cache directory from my initial project and the imports started working in both projects. |
Looks like I was wrong. I restarted my computer and now my custom module cannot be resolved again. I remember I had been messing about with my pth file. Normally I have 4 paths in it which are all loaded into Python without issue. While filling out this bug report, I had removed all but 1, which was when VS Code started working. I had to add the other 3 paths back again later for my scripts to function properly. Which is why it stopped working and I didn't notice until VSCode was restarted. project.pth = I've done some testing:
It looks like VSCode gets confused when pth files have more than one entry. |
New issue: The error message is: Also notice that the message is not showing the correct module import path. The lib directory is not a module as it does not have a __init__.py file. If I change the path of the relative import to a full module import of 'from custom_module.sub_module.rest import Rest', then the error goes away. The path in the message is also missing the current module. It's almost like it the path it is looking for is shifted too far up the tree. Instead of looking in custom_module/sub_module/rest, I think it is looking for lib/custom_module/rest which doesn't exist. Developer Console output
|
Thanks for bringing this up @jaredcm, an interesting problem! I didn't know about I can repro this issue with the latest stable VS Code (1.30.2) + latest extension (2018.12.1), using the Jedi language server ( Workaround: The Microsoft Python Language Server (as of version 0.1.72.0, or greater) Adding more than one entry.Actually, on second look I have found that this is not handled properly by the Microsoft Python Language Server as I'd originally stated (at least version 0.1.72.0 doesn't) as I'd left out the multiple lines in the Indeed, if you add a Here's the sample workspace: Here's the sample
|
Closing in favour of microsoft/python-language-server#537. If you experience this with jedi, please file an issue on the jedi repo |
Environment data
Expected behaviour
Adding custom locations to
.pth
file should be resolved by the Python intellisense.Actual behaviour
I have a custom package that is in a subdirectory of my project. I want Python to resolve it as if it were an installed module.
The project is setup as such.
os_path
-|+- project_dir
--|+- bin
---|+- test.py
--|+- lib
---|+- custom_module
----|+- __init__.py
In the python 3.7 site-packages directory I have a
.pth
file with something like the following contents.{os_dir}/{project_dir}
{os_dir}/{project_dir}/lib
In Python this resolves without issue and so I can do.
If I create a test.py file in the project's bin directory like so:
In VSCode I get unresolved import:
{
"resource": "/{os_path}/{project_dir}/bin/test.py",
"owner": "generated_diagnostic_collection_name#0",
"code": "unresolved-import",
"severity": 4,
"message": "unresolved import 'custom_module'",
"source": "Python",
"startLineNumber": 2,
"startColumn": 8,
"endLineNumber": 2,
"endColumn": 21
}
If I switch the import to lib.{custom_module} then the module resolution works. I'm guessing this is because VSCode adds the workspace to the PYTHONPATH.
Steps to reproduce:
.pth
file and add the full OS path to the lib directory.Logs
Output for
Python
in theOutput
panel (View
→Output
, change the drop-down the upper-right of theOutput
panel toPython
)Log (Window)
Output from
Console
under theDeveloper Tools
panel (toggle Developer Tools on underHelp
)The text was updated successfully, but these errors were encountered: