Why does PyCharm give unresolved reference errors on some Numpy imports?

34,343

Solution 1

The reason you are getting this is because of PyCharm's static analysis. Now, what Python does is use static skeletons (some are pre-generated and some are generated) to give you the analysis. Take a look at the pre-generated skeletons here -> https://github.com/JetBrains/python-skeletons

This might be solved, by enabling the following:

enter image description here

However, if that does not work:

enter image description here

which will block off the error, it will appear as a comment above the line.

Solution 2

The Python configuration is specified in (at least) two places: Run | Edit Configurations | Python | Python Interpreter, and File | Settings | Project | Project Interpreter. My mistake was I did not set the correct Python installation in the File | Settings .... Hence, it was referring to a Python configuration that did not have the import installed (e.g. NumPy).

After I set these two locations to point to the same, correct Python installation, I did a File | Invalidate Caches / Restart, then it was fine.

A third place to check is File | Default Settings... | Project Interpreter and make sure it matches the other settings.

Solution 3

The following often helps to solve false-positive unresolved references

File | Invalidate Caches

Solution 4

PyCharm developer posted a workaround for one possible cause of inspection failure:

https://youtrack.jetbrains.com/issue/PY-32029

Gist of it - inspection may fail if you have a venv folder in the project directory. Right click it, mark directory as excluded.

Solution 5

In PyCharm's Project tool window, right-click on the directory and select Mark Directory As -> Sources Root.

Share:
34,343
flutefreak7
Author by

flutefreak7

I'm an Aerospace Engineer working for NASA doing solid rocket motor ballistics analysis. While software development is not in my job title, I love programming and try to use it to make my job and the jobs of my team members easier by developing analysis tools. My programming experience began like many Engineers with Excel VBA and Matlab, which expanded to Python and VB.Net. My job also requires I use a number of legacy FORTRAN analysis codes. I also program my TI-89, mod my phone/router, and make advanced spreadsheets documenting... everything. Discussions abound extolling the advantages of Python over Matlab. I do continue developing in Matlab because it’s used by my coworkers and is great with data. These days (2017) almost everything new I do is in Python with Matlab and Excel reserved for 'customers' who need tools on those platforms. I found Python whilst seeking freedom from commercial tools. Python is extensively used and well supported in the science and engineering community with tools like Scipy, Numpy, Matplotlib, Pandas, Spyder, Mayavi, etc, and the ease with which libraries can be utilized make Python immediately useful. Python is also learnable, powerful, brilliantly designed, philosophy focused, and well supported by an internet-spanning community. My current Python projects utilize PyQt to develop frontends and data exploration tools for CLI analysis codes. I've also begun to replace VBA with VB.Net for developing advanced Excel Add-Ins (with help from Excel-DNA). After reading plenty of discussions and flame wars online about VB.Net vs. C#, I finally decided that VB.Net is exactly where I want to be despite the popularity of C# in computer science and internet arenas. VB actually has a really strong heritage in engineering circles and will serve me better for rapid application development, Excel Add-Ins, and has a chance of being maintainable by my coworkers, who have experience in VBA and VB6. I also prefer VB.Net's more human-readable syntax, it has a lower learning curve for me since I'm very proficient with VBA, and recent additions to the language give it list and dictionary literals that are almost Pythonic! While the more lax nature of the compiler might make some programmers cry a little (Option Explicit turned off, late-binding, undeclared variables, etc.), it sure speeds up development when I don't have time to write perfect software. Most engineering code is some ugly hacked together version of good enough, and VB.Net fits that so much better than C#. I'll do my elegant production code in Python and my Excel Hack-foo in VBA or VB.Net. Outside work I spend most of my time with my awesome wife, our two cats, and (after Oct 2017...) my son. I enjoy Star Trek (NASA's the closest thing I could find to Starfleet Academy), video games, programming, jazz, playing the flute (yes.... jazz flute!), working on the house, and being involved with the young adult group at my church.

Updated on November 14, 2020

Comments

  • flutefreak7
    flutefreak7 over 3 years

    The following line in PyCharm is flagged by on-the-fly inspection with unresolved reference errors for each import. (They are underlined red.)

    from numpy import tan, arcsin, arccos, arctan
    

    However the following imports do not cause any error/warning:

    from numpy import sin, cos, arctan2, sqrt, cross, pi
    

    The code in which I use these imports runs fine without any errors or warnings. I generally rely on PyCharm's red errors as a warning that my code is broken and will not run, but in this case PyCharm is wrong.

    Why are some of numpy's functions recognized by PyCharm's introspection and others aren't?

    Current Versions:

    • Windows 7 64-bit
    • Python 2.7.5
    • PyCharm 3.1.2
    • Numpy 1.8

    Thanks!