Very frustrated--PyCharm says local import is unresolved reference

Answered

Hi,

 

PyCharm is claiming a local import is an unresolved reference, but when I run the script through Run/Debug mode (yes, while the import is still highlighted as red) the script runs fine.  I tried restarted PyCharm to see if the error would go away but this hasn't resolved it.  I am using the follow PyCharm:

PyCharm 2018.1.4 (Professional Edition)

The structure of my project is like this:

 

root/scripts1/foo.py

root/scripts1/bar.py

In bar.py, I have "import foo".  The current working directory for bar.py is also set to root/scripts1/.  However, PyCharm complains that "import foo" is an unresolved reference and underlines the whole thing in red.  Nevertheless, I can still execute bar.py with the foo import when using the Run/Debug mode.  PyCharm wants me to do "from . import foo"  but this actually breaks my script!  root/scripts1 is not a Python module/package and does not have an __init__.py (or in the parent dir either).  

Why is this happening and how do I fix it?  

 

10 comments
Comment actions Permalink

have you tried the '.' immediately before foo?

import .foo

You can also suppress warnings: https://www.jetbrains.com/help/pycharm/suppressing-inspections.html

0
Comment actions Permalink

I am getting the same error, I was using version 2017.2, now updated Pycharm to 2018.3.5 and still get the same error. Indeed very frustrating and more frustrating is the lack of answer for this problem. Any news?

1
Comment actions Permalink

@Pedro Quintas

The latest version is 2019.1.1 , please update (https://www.jetbrains.com/pycharm/download), and if the issue is still reproduced, provide the code snippet which can be used to reproduce the issue.

0
Comment actions Permalink

Hello, I am using PyCharm community version:

PyCharm 2019.2.5 (Community Edition)
Build #PC-192.7142.56, built on November 19, 2019
Runtime version: 11.0.4+10-b304.77 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0
GC: ParNew, ConcurrentMarkSweep
Memory: 974M
Cores: 4
Registry:
Non-Bundled Plugins: CMD Support, izhangzhihao.rainbow.brackets, org.intellij.plugins.markdown, some.awesome

I have a file called "p01.py", and I import it from a file in the same directory called "p02.py". I am still getting this issue ("No module named p01", in file p02.py). 

Links: p01.py, p02.py

Thank you!

0
Comment actions Permalink

@Dan Katzuv

>I have a file called "p01.py", and I import it from a file in the same directory called "p02.py". I am still getting this issue ("No module named p01", in file p02.py). 

Are those files located in sub-directory (not project root)? If yes, please try marking this directory as Sources: https://www.jetbrains.com/help/pycharm/configuring-folders-within-a-content-root.html#mark

9
Comment actions Permalink

Andrey Resler Yes! Your suggestion to set the project sub-directory with the python files as "Sources Root" is what finally allowed PyCharm to acknowledge the imports. That totally makes sense. Thanks!

2
Comment actions Permalink

Ah! I have been having this problem for a long time too and it means that you miss out on all of the autocomplete and inspections... (with ugly, red underlined imports...).. Marking the top level directory as "Sources Root" has done it for me too! Thank goodness for that!

2
Comment actions Permalink

This is still affecting me. Project structure is as follows:

project-name -> 
src ->
package ->
__init__.py
script.py
other_package ->
__init__.py
other_script.py

If I try to do this inside script.py:

from src.other_package import other_script

I get an "Unresolved reference 'other_script' "

This means I don't get any autocomplete features/help with parameter names, etc.

If I remove the __init__.py from other_package, this problem goes away but then setuptools doesn't treat other_package as a package!

Any ideas?

0
Comment actions Permalink

I'm just a user same as you but I believe you need to nest some_package inside of package to do relative imports as you are wanting to do. It's just how python works I'm afraid.

So you need to change the structure of your app in line with a single heirarchy / tree and only import from same level of tree or higher. You leaves cannot import from other leaves of the tree.

Hope this helps :)

1
Comment actions Permalink

Then you should use relative imports after restructuring. Like this

from .other_package import other_script

Everything will work out better for you that way.

0

Please sign in to leave a comment.