Import unresolved references

Answered

I have import statements at the beginning of my PY file with red squiggly lines under them - they do not cause the script to fail). When I hover over the import, is says:

"unresolved reference - the inspection detects names that should resolve but don't. Due to dynamic dispatch and duck type, this is possible in a useful but limited number of cases. Top-level and class-level items are supported better than some instance items."

This is not fatal since the script typically runs fine but I occasionally get weird errors with imports. Why does this happen? How can I correct it? Thank you!

4
42 comments

Hi Paul! Sorry for keeping you waiting.

Most likely these false positives are caused by wrong source roots configuration. Could you please give us more details about where it happens? I think screenshots of a file with such error and "Project" tool window in the corresponding project should be enough to find out the reason. Also, which Python interpreter do you use?

2
Avatar
Permanently deleted user

I have the same problem as Paul, except my scripts don't run at all. Additionally, despite having __init__.py files in each package, the symbol for it  - the f
older icon with the dot in the corner of it, doesn't show. Please refer to the image below:

 

2
Avatar
Permanently deleted user

Also the sources root has been marked, please feel free to point out anything wrong with it. 

Thanks a lot.

-1

Hi there,

You need to mark gci/src/gci directory as source root in order to import policyengine. Think of source roots as of additional entries in sys.path: when we're resolving qualified names in imports, we start looking for the first component right inside them, not skipping intermediate directories in-between somehow.

This is also the reason why that directories are not displayed as packages in "Project" tool window, for each of them there should be a chain of valid importable parent packages up to the first source root/content root, and for e.g. policyengine it stops prematurely at gci since it doesn't contain __init__.py (the latter is not necessary in Python 3.3+ because of namespace package, but directory names still must be valid identifiers).

2
Avatar
Permanently deleted user

Since I switched from PyCharm to IntelliJ IDEA Ultimate, these annoying but nonfatal warnings seem to show up more often. Or, maybe it has something to do with upgrading to Python v3.6. Fortunately, these warnings are easy to avoid. Just check the Install to user's site packages box before you install packages using the IDE. 

1

I have the same issue. The same project, open in IntelliJ Ultimate and all I see is red. Open in PyCharm... less red.

IntelliJ Ultimate

 

PyCharm

 

I've tried just about every cure. Nothing makes my red go away.

0

Which version of IntelliJ IDEA/Python plugin do you use? Also, which interpreter version is configured for the project?

0

Mikhail, thanks for the offer to help.

I'm running version 2018.1.181.4203.550 of the Python plugin

Python interpreter is version 3.6

 

Very excited to get rid of this:

0
Avatar
Permanently deleted user

I have the same problem. My problem started when I upgraded to PyCharm 2018.1. Note that the problem does not exist in PyCharm 2017.1 using the same interpreter and the same source files (see images). Not only do the references not resolve my CTRL-click click through does not work either. Went through 2017.2 and 2017.3 without this issue so the problem was definitely introduced in 2018.1. Using the same Python 2.7.13 VE in both IDEs. I have wiped out the project and re-loaded it numerous times. I have cleared the PyCharm setting using "invalidate cache and restart" as well as wiping out the ".PyCharm2018.1" folder and recreating it. I have also tried setting the source folders in 2018.1 (something I did not need to do in 2017.1). Nothing I have tried corrects the issue. I realize that the added support for the newest Python "could" have something to do with it but the version of Python we are using is out of my control. There must be some way to get the imports to resolve correctly without massive changes to the project. The __init__ files are not set within the project but they did not need to be for 2017.1 and I don't think the IDE should drive our project structure. Please provide steps to resolve this or a patch. Thank you.

 

PyCharm 2018.1

 

PyCharm 2017.1

0

That's PY-29338, we are investigating the problem, I am sorry for the inconvenience!

0

Hm, I think I was wrong, PY-29338 covers only the Python plugin in IDEA. Could you please check 2018.1.1 RC build? There're related to resolving changes. If it will not help - please create a new ticket in our bug tracker and provide more details about the project structure.

0
Avatar
Permanently deleted user

Pavel, It does appear that the issue is corrected in the 2018.1.1 RC build. That is, all of my imports are resolving as they did trhough all of the 2017 builds and the click through (CTRL-click) is working again. For some reason the version number is not displaying at the top [you can see in the previous screenshots the version was at the end of the string at the top "... - PyCharm 2017.1" and it is not at the top of the 2018.1 version "... - PyCharm"] but that is not a functional issue. Thank you for your help.

0
Avatar
Permanently deleted user

I had a similar issue, changing the sources root directory fixed it for me.

0

Like all the others I have the same problem. But when I open a file standalone, the imports work fine! It's only when opening the file as part of a project that the imports fail.

0

Could you please provide a project tree screenshot? Feel free to create a ticket in our bug tracker: https://youtrack.jetbrains.com/issues/PY

0
Avatar
Permanently deleted user

Hi have a problem importing

0

Hi, the warning just states cv2 is not used in the code. It is not an error, PyCharm is able to resolve the module.

0
Avatar
Permanently deleted user

Thanks but generally it does not work it gives me error shown below. Should I download tensorflow AVX

WARNING:tensorflow:From C:\Users\Lenovo\AppData\Local\Programs\Python\Python36\lib\site-packages\tflearn\initializations.py:119: UniformUnitScaling.__init__ (from tensorflow.python.ops.init_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.initializers.variance_scaling instead with distribution=uniform to get equivalent behavior.
WARNING:tensorflow:From C:\Users\Lenovo\AppData\Local\Programs\Python\Python36\lib\site-packages\tflearn\objectives.py:66: calling reduce_sum (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version.
Instructions for updating:
keep_dims is deprecated, use keepdims instead
2018-07-27 15:12:06.454304: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2

0

This looks like some warnings from tensorflow itself, isn't the same output shown in the terminal outside of PyCharm when running the script?

0
Avatar
Permanently deleted user

Yes

 

0

I think the deprecation warnings are safe to ignore, you can disable this verbose logging with tf.logging.set_verbosity(tf.logging.ERROR) (suggestion from https://github.com/keras-team/keras/issues/8742) in case you find them annoying.

The AVX2 error is discussed in this StackOverflow question https://stackoverflow.com/questions/47068709/your-cpu-supports-instructions-that-this-tensorflow-binary-was-not-compiled-to-u Could you please take a look? There're some suggestions about it.

As a side note, I believe those problems are not caused by PyCharm.

0
Avatar
Permanently deleted user

This issue has been a thorn in my side ever since we did a repository re-org. I was able to access individual library modules under a particular folder.  I would reference them using the format  foldername.module (ie. lib.database).  Now It can only reference the module directly (i.e. database) but if the folder name is provided it does not work.  Because I am a contributor rather than a sole owner so I do not have the option to made changes across the board to accommodate this.  Below is what my project structure looks like.

 

0

Yes, but this behavior is expected since the directory "Caspy" containing "lib" package is not marked as a source root. Remember such directories act like additional entries to PYTHONPATH, without them we can only resolve absolute imports of project modules where qualified names begin right from the project root.

1
Avatar
Permanently deleted user

Thank you Mikhail, that worked like a charm.   

0

 I'm having the same problem and have found out how to change things but I cannot change things to the desired state.  So my project hiararchy is like this:

 

/inference_engine/proofs

      file1.py

      file2.py

     /other

             file3.py

             file4.py

 

When I set the content root to

/inference_engine/proofs

 

I can import functions from file2.py when I'm in file1.py without the red lines appearing.  But when I'm in file3.py I can import functions from file4.py but the red lines appear.  So if I change the content root to just

inference_engine/

 

then I red lines in files 1- 4.  How do I set the content root so that the red lines do not appear in neither files 1 and 2 as well as files 3 and 4?

 

Also I'm aware of the sys.path.append method but I don't want to keep using that.  An error is not being thrown I just want the red lines to go away without having to write a lot more code.

 

 






0

Hi Kyle, you need to mark both proofs and other as Source Roots.

0

Thanks! That did it.

0

I'm having the same problem again.  I tried and failed to fully delete pycharm and redownload it. in any case, when i redownloaded it, I encountered the same problem. This time a bunch of modules that once were working fine are no longer importable.  When I open the project from one level up the heirarchy the problem goes away but then I have too many files to deal with and it's very hard to keep myself organized and find things.  Here is a screen shot

 

When I go to the content folder I don't see much that I can do there to improve the situation:

 

0

ok, I was able to add the /Library/frameworks/python3.6 etc folder to my content root and that solved the problem. 

0
Avatar
Permanently deleted user

Hello! Could you please explain what is the reason of this warning?

 

Thank you!

0

Please sign in to leave a comment.