F401 erroneously raised with "import as"
Bug #1589186 reported by
mforbes
This bug affects 1 person
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
Pyflakes |
Incomplete
|
Undecided
|
Unassigned |
Bug Description
The following code raises an F401 error.
```
import scipy.optimize
import scipy as sp
print(sp.optimize)
```
I don't see any obvious workaround. I used to do:
```
import scipy.optimize
sp = scipy
import numpy
print(sp.optimize)
```
but now flake8 correctly flags the line "sp = scipy" as E402 module level import not at top of file, so this workaround is less useful. (One could put all the module assignments at the end, but this is not very good for reading the code.)
I seem to recall discussing this at some point but cannot find the old discussion.
To post a comment you must log in.
So the flake8 error F401 is pyflakes reporting the following for line 1: 'scipy.optimize' imported but unused.
Which is correct for the sample code you have given.
scipy.optimize is unused, as the object with name 'scipy' is not accessed on line 2; instead the import on line 2 creates a new object 'sp' which just happens to be a module called 'scipy' in the import lookup system.
So I dont believe this is a bug, based on the sample code provided.
I am guessing that you dont really want to print scipy.optimize ? You are trying to hide the error, by doing a dummy usage?
The following will do the import, and hide the pyflakes error
import scipy.optimize as _
import scipy as sp
del _
def x():
return sp.foo()
If you really need to print or access scipy.optimize, the this works
import scipy.optimize
import scipy as sp
print(scipy. optimize)
def x():
return sp.foo()
But maybe your real code cant be solved those ways, in which case let me know the project and I'll take a look at the problem with real code.