Giter Club home page Giter Club logo

findimports's Introduction

FindImports

FindImports extracts Python module dependencies by parsing source files. It can report names that are imported but not used, and it can generate module import graphs in ASCII or graphviz formats.

A distinguishing feature of findimports used to be that it could parse doctest code inside docstrings.

Note that not all cases are handled correctly, especially if you use 'import foo.bar.baz'.

If you need to find unused imports in your codebase, I recommend Pyflakes instead -- it's better maintained and more reliable. For import graphs consider pydeps.

Misc

Home page: https://github.com/mgedmin/findimports

Licence: MIT (https://mit-license.org/)

buildstatus appveyor coverage

findimports's People

Contributors

dg-pb avatar jepler avatar mgedmin avatar nicolas-reyland avatar takos22 avatar zzzeek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

findimports's Issues

Namespace packages

I'm trying to use findimports with namespace packages (i.e. those which don't have an __init__ in their directory tree) and I'm running into some issues.

First, the directory structure:

$ tree
.
├── Makefile
├── moat
│   └── main
│       └── __init__.py
├── README.md
├── setup.cfg
└── setup.py

There's a ../util directory alongside this one which has much the same structure:

 tree ../util
../util
├── Makefile
├── moat
│   └── util
│       ├── __init__.py
│       ├── times.py
│       └── _yaml.py
├── README.rst
├── setup.cfg
└── setup.py

OK. So when I run findimports on this, I get

$ PYTHONPATH=.:../util/ findimports --ignore-stdlib 
main.__init__:
  anyio
  asyncclick
  util
setup:
  setuptools

Now this is obviously suboptimal, as there's no "util" package and the toplevel isn't "main" either. I have to do

  • touch moat.__init__.py
  • touch ../util/moat/__init__.py

to get a more reasonable output of

moat.__init__:
  
moat.main.__init__:
  anyio
  asyncclick
  moat.util
setup:
  setuptools

To fix the requirement for the first touch statement I would like an option that tells findimports that it should assume that every dictionary it finds (or that's named on the command line) might be the root of a namespace package. This way the src/whatever style of laying out your source repository would still work unchanged.

Imported modules should be recognized no matter whether they're part of a namespace, i.e. the second touch should not be necessary even if I don't use that special new option.

'could not find datetime'

My Jenkins runs findimports, which produces errors of the form

src/foo.py: could not find datetime
src/bar.py: could not find datetime.date
src/baz.py: could not find datetime.datetime
src/qux.py: could not find datetime.timedelta

Why would it not be able to find the stdlib module?

I cannot reproduce if I run findimports locally on the same project.

Default use-case doesn't seem to be covered.

Correct me if I'm wrong but I feel that this package doesn't easily cover a default use case which is:

Given I have worked on a project for an amount of time and I have installed different external packages:

  • run the package locally and display which external packages are really needed
  • optionally produce a requirements file
  • optionally add current version as from pip show

Thank you for this package,

To all contributors: Approval request for changing license. GPL -> MIT

Hello all contributors,

It can be seen that there were considerations for MIT license in the beginning of development of this library. However, it was licensed under GPL.

I have raised a request to the owner of this library regarding license change and he has kindly agreed.

However, for this to happen approval is needed from every non-trivial contributor.

Implications fo this change are as follow:

  • All new versions are simply licensed under the new license.
  • Anyone who needs code licensed under the old license (GPL) can fork the earlier version.

So I am raising this issue for contributors to approve (or not) this request for license change.

Approvals needed for this to happen:

  1. @mgedmin
  2. @Nicolas-Reyland

Approvals that would make this transition easier:
3. @takos22
4. @jepler
5. @zzzeek
6. @timhoffm

It would be much help if contributors commented their position in this issue.

Regards,
Dominykas

Error when both -c and -n options used

I've just got something like that:

 findimports -dnc <the_package_dir>
Traceback (most recent call last):
  File "/usr/local/bin/findimports", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python2.7/dist-packages/findimports.py", line 832, in main
    getattr(g, opts.action)()
  File "/usr/local/lib/python2.7/dist-packages/findimports.py", line 703, in printImportedNames
    print("  %s" % "\n  ".join(imp.name for imp in module.imported_names))
AttributeError: 'ModuleCycle' object has no attribute 'imported_names'

Also effect of mixing -n with -d (without -c) is not described in help.
When used in following order: -dn then -d is ignored and with-nd then -d is ignored.

no documentation

I was not able to find any documentation in README file about how to install and run the tool, only its description. Adding some steps may be a good idea.

UTF-8 encoded file with byte-order-marker cannot be parsed

$ python3 -c 'with open("example.py", "wb") as f: f.write("\ufeffimport sys\n".encode("utf-8"))'
$ file example.py
example.py: UTF-8 Unicode (with BOM) text
$ python3 example.py  # succeeds, producing no output
$ findimports example.py 
Traceback (most recent call last):…
  File "/usr/lib/python3.7/ast.py", line 35, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)
  File "example.py", line 1
    import sys
          ^
SyntaxError: invalid character in identifier

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

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 !

Collapse for non-package mode

What I would like is to be able to control collapse of sub-packages individually for external dependencies and non-external dependencies in non-package mode.

Maybe there is an easy way to implement it?

By the way.
Nicely written package. This is my goto option for dependency graphs now. Although it's simpler than some alternatives, but it is nicely written and works fast.

Also,
I was just wandering if any work is planned on making nicer outputs for graphviz and/or clustering.

FileNotFoundError: __future__.py

Just met that bug, not a big one because I think my case is special.
In my project folder there is another virtualenv folder and when trying to parse it :

Traceback (most recent call last):
  File "/home/me/myVirtualenv/bin/findimports", line 11, in <module>
    sys.exit(main())
  File "/home/me/myVirtualenv/lib/python3.6/site-packages/findimports.py", line 822, in main
    g.parsePathname(fn)
  File "/home/me/myVirtualenv/lib/python3.6/site-packages/findimports.py", line 427, in parsePathname
    self.parseFile(os.path.join(root, fn))
  File "/home/me/myVirtualenv/lib/python3.6/site-packages/findimports.py", line 454, in parseFile
    module.imported_names = find_imports(filename)
  File "/home/me/myVirtualenv/lib/python3.6/site-packages/findimports.py", line 319, in find_imports
    with open(filename) as f:
FileNotFoundError: [Errno 2] No such file or directory: './localVenv/lib/python3.5/__future__.py'

Is it expected?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.