Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pylab import replaced by 'py' when matplotlib is not installed but py is #10

Closed
chaen opened this issue Oct 31, 2019 · 5 comments
Closed
Labels

Comments

@chaen
Copy link

chaen commented Oct 31, 2019

Hi,

The pylab import is seen as a py module

Create a file with just that import

[chaen]$ cat /tmp/test.py 
from pylab import hist

Run findimports on it

[chaen]$ findimports /tmp/test.py 
test:
  py

The installed version is 1.5.1

[chaen]$ pip freeze | grep findimports
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
findimports==1.5.1

Any idea what is going wrong ?
Thanks !

@mgedmin
Copy link
Owner

mgedmin commented Oct 31, 2019

Oh, this is interesting!

I cannot reproduce it:

$ virtualenv -p python2.7 /tmp/py27

$ /tmp/py27/bin/pip install findimports
Successfully installed findimports-1.5.1

$ cat /tmp/test.py
from pylab import hist

$ /tmp/py27/bin/findimports /tmp/test.py
/tmp/test.py: could not find pylab.hist
test:
  pylab.hist

$ /tmp/py27/bin/pip install matplotlib
Successfully installed backports.functools-lru-cache-1.5 cycler-0.10.0 kiwisolver-1.1.0 matplotlib-2.2.4 numpy-1.16.5 pyparsing-2.4.2 python-dateutil-2.8.0 pytz-2019.3 six-1.12.0 subprocess32-3.5.4

$ /tmp/py27/bin/findimports /tmp/test.py
test:
  pylab

Can you tell me what findimports /tmp/test.py -d would print? For me it looks like

digraph ModuleDependencies {
  node[shape=box];
  mod0[label="test"];
  node[style=dotted];
  extmod0[label="pylab"];
  mod0 -> extmod0;
}

and the reason I ask is I want to make sure it's not the plaintext output getting truncated midword (although I've no idea why it would be truncated midword).

Another test would be to find out what Python interpreter is used for findimports with

$ head -n 1 $(which findimports)

and then run that interpreter and in it try

>>> import pylab
>>> pylab.__file__

which for me produces '/tmp/py27/local/lib/python2.7/site-packages/pylab.pyc'.

Actually, I have a Theory. Bear with me a moment.

@mgedmin
Copy link
Owner

mgedmin commented Oct 31, 2019

Reproduced!

  • virtualenv /tmp/py
  • /tmp/py/bin/pip install py findimports
  • /tmp/py/bin/findimports /tmp/test.py

produces the same output as your code.

And the reason is that in this loop:

findimports/findimports.py

Lines 502 to 509 in caa0e71

while name:
candidate = self.isModule(name, extrapath)
if candidate:
return candidate
candidate = self.isPackage(name, extrapath)
if candidate:
return candidate
name = name[:name.rfind('.')]

I'm trying to take off the last part of the dotted name with

        name = name[:name.rfind('.')]

and name.rfind('.') returns -1 when there are no dots in it, which cuts off the last character, one by one, until pylab becomes py, which exists in your site-packages!

@mgedmin mgedmin changed the title pylab import replaced by 'py' pylab import replaced by 'py' when matplotlib is not installed but py is Oct 31, 2019
@mgedmin mgedmin added the bug label Oct 31, 2019
@mgedmin
Copy link
Owner

mgedmin commented Oct 31, 2019

(In my defense I originally wrote this code in 2003. Python 2.3 was the modern version. str.rindex ... existed, so I've no excuse!)

@mgedmin
Copy link
Owner

mgedmin commented Oct 31, 2019

Released findimports 1.5.2 with the fix.

@chaen
Copy link
Author

chaen commented Nov 1, 2019

Woh, thanks a lot, impressive response speed :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants