numpy resolution problem

Answered
PycharmProblem.png


PyCharm is failing to resolve imports from numpy. If I write:
ImportFail
import numpy as np
import numpy.core.umath as np2


def my_exp(t):
    return np.exp(t)


def my_exp2(t):
    return np2.exp(t)


PyCharm resolves np2.exp() correctly but fails to find np.exp() with a "Cannot find reference 'exp' in __init__.py".
After working out that the __init__.py in question is the numpy one (it makes sense but there are a lot of __init__.py s floating around) I think I have narrowed the problem down to how the numpy skeleton is created. The original numpy __init__.py is quite elaborate and imports several submodules into the main namespace. In particular there is the line:

__all__.extend(core.__all__)


which is I think responsible for np.exp() running. However despite the numpy __init__.py being 216 lines long, the stub in the skeleton is empty. Is there a way around this? I use numpy in practically all my code and it is for sprawling libraries like this that code completion and resolving on the fly are most useful so I would really like to know how to fix this.

I'm using PyCharm 4.0.4 Professional with numpy 1.9.1 in an Anaconda virtual environment. The interpreter seems to work correctly, PyCharm finds the correct packages in the correct locations and the normal np.exp works from the console.

EDIT: So I've looked a bit further into skeletons and am more confused but possibly closer to knowing how to fix the problem. The GitHub repository for python skeletons has a non-empty skeleton for numpy's __init__.py which dates to July last year but references version 1.8. It seems that I should clone the github repository and edit the skeleton and can probably manage to do that if there is only one numpy __init__.py but if that were the case, how have I got an empty one? Is something picking out versions? I have now checked that the github skeletons-helpers version of the numpy __init__.py is in my folder but am not sure how to make it, and therefore any update I might make, available to my project which is referring to an empty file numpy/__init__.py stub.

EDIT EDIT: This appears to be the same issue as bugs PY 13124 and PY 14971. The former of which has been open since May last year.

EDIT EDIT EDIT: And also PY 11316 from November 2013
9 comments
Comment actions Permalink

This bug appears to be back with Intellij IDEA ULTIMATE 2017.3.

Code inspection can't find reference 'arange', 'dot', 'maximum', 'sqrt', 'square', 'zeros', etc in '__init__.py'

3
Comment actions Permalink

Same issue here with arange - PyCharm 2018.1

1
Comment actions Permalink

I, too, have the same issue with np.arange on PyCharm Professional 2018.2.5. Is this ever going to be fixed? It is so annoying.

1
Comment actions Permalink
How does numpy.core import exp into its namespace?
0
Comment actions Permalink
As I understand it exp is defined in numpy.core.ufunc which is imported to the numpy namespace in numpy/__init__.py (my 1.9.1 version attached) either by
from . import core
from core import *

on lines 181 and 182
or
__all__.extend(core.__all__)

online 208.

So the more complicated "core" import statement is a completely ordinary import which works while calling numpy.exp involves a much more complex import process which hasn't translated to successful code navigation in the binary skeletons. I believe I am running exactly the same code in each case but the normal invocation fails as described as far as PyCharm code analysis is concerned.
0
Comment actions Permalink
Hi bunbury,

this is related to PY-11316 and will be fixed in next PyCharm release
0
Comment actions Permalink
Great news and good to know.
0
Comment actions Permalink

Same here, the bug is back and it makes the code hard to clean, as warnings are showing up all the time. 

0
Comment actions Permalink

Hi all! Please vote & comment for PY-23521. Unfortunately, I had not much luck reproducing it so any tips are greatly appreciated!

0

Please sign in to leave a comment.