List dependencies in Python

Posted on

Question :

List dependencies in Python

What is the most efficient way to list all dependencies required to deploy a working project elsewhere (on a different OS, say)?

Python 2.7, Windows dev environment, not using a virtualenv per project, but a global dev environment, installing libraries as needed, happily hopping from one project to the next.

I’ve kept track of most (not sure all) libraries I had to install for a given project. I have not kept track of any sub-dependencies that came auto-installed with them. Doing pip freeze lists both, plus all the other libraries that were ever installed.

Is there a way to list what you need to install, no more, no less, to deploy the project?

EDIT In view of the answers below, some clarification. My project consists of a bunch of modules (that I wrote), each with a bunch of imports. Should I just copy-paste all the imports from all modules into a single file, sort eliminating duplicates, and throw out all from the standard library (and how do I know they are)? Or is there a better way? That’s the question.

Asked By: RolfBly


Answer #1:

Scan your import statements. Chances are you only import things you explicitly wanted to import, and not the dependencies.

Make a list like the one pip freeze does, then create and activate a virtualenv.

Do pip install -r your_list, and try to run your code in that virtualenv. Heed any ImportError exceptions, match them to packages, and add to your list. Repeat until your code runs without problems.

Now you have a list to feed to pip install on your deployment site.

This is extremely manual, but requires no external tools, and forces you to make sure that your code runs. (Running your test suite as a check is great but not sufficient.)

Answered By: RolfBly

Answer #2:

pipreqs solves the problem. It generates project-level requirement.txt file.

Install pipreqs: pip install pipreqs

  1. Generate project-level requirement.txt file: pipreqs /path/to/your/project/
  2. requirements file would be saved in /path/to/your/project/requirements.txt

If you want to read more advantages of pipreqs over pip freeze, read it from here

Answered By: 9000

Answer #3:

On your terminal type:

pip install pipdeptree
cd <your project root>
Answered By: Haifeng Zhang

Answer #4:

The way to do this is analyze your imports. To automate that, check out Snakefood. Then you can make a requirements.txt file and get on your way to using virtualenv.

The following will list the dependencies, excluding modules from the standard library:

sfood -fuq | sfood-filter-stdlib | sfood-target-files 

Related questions:

Get a list of python packages used by a Django Project

list python package dependencies without loading them?

Answered By: Venu Gopal Tewari

Answer #5:

I would just run something like this:

import importlib
import os
import pathlib
import re
import sys, chardet
from sty import fg


dependenciesPaths = list()
dependenciesNames = list()
paths = sys.path
red = fg(255, 0, 0)
green = fg(0, 200, 0)
end =

def main(path):
        print("Finding imports in '" + path + "':")

        file = open(path)
        contents =
        wordArray = re.split(" |n", contents)

        currentList = list()
        nextPaths = list()
        skipWord = -1

        for wordNumb in range(len(wordArray)):
            word = wordArray[wordNumb]

            if wordNumb == skipWord:

            elif word == "from":
                currentList.append(wordArray[wordNumb + 1])
                skipWord = wordNumb + 2

            elif word == "import":
                currentList.append(wordArray[wordNumb + 1])

        currentList = set(currentList)
        for i in currentList:

        print("Found imports in '" + path + "'")
        print("Finding paths for imports in '" + path + "':")

        currentList2 = currentList.copy()
        currentList = list()

        for i in currentList2:
            if i in dependenciesNames:
                print(i, "already found")


                    fileInfo = importlib.machinery.PathFinder().find_spec(i)



                except AttributeError as e:
                    # print(red, "Odd noneType import called ", i, " in path ", path, end, sep='')

        print("Found paths for imports in '" + path + "'")

        for fileInfo in currentList:

    except Exception as e:

if __name__ == "__main__":
    # args
    args = sys.argv

    if len(args) == 2:
        p = args[1]

    elif len(args) == 3:
        p = args[1]

        open(args[2], "a").close()
        sys.stdout = open(args[2], "w")

        print('PyDependencies <InputFile>')
        print('PyDependencies <InputFile> <OutputFile')


    if not os.path.exists(p):
        print(red, "Path '" + p + "' is not a real path", end, sep='')

    elif os.path.isdir(p):
        print(red, "Path '" + p + "' is a directory, not a file", end, sep='')

    elif "".join(pathlib.Path(p).suffixes) != ".py":
        print(red, "Path '" + p + "' is not a python file", end, sep='')

        print(green, "Path '" + p + "' is a valid python file", end, sep='')


    deps = set(dependenciesNames)


Answered By: nighthawk454

Answer #6:

I found the answers here didn’t work too well for me as I only wanted the imports from inside our repository (eg. import requests I don’t need, but from my.module.x import y I do need).

I noticed that PyInstaller had perfectly good functionality for this though. I did a bit of digging and managed to find their dependency graph code, then just created a function to do what I wanted with a bit of trial and error. I made a gist here since I’ll likely need it again in the future, but here is the code:

import os

from PyInstaller.depend.analysis import initialize_modgraph

def get_import_dependencies(*scripts):
    """Get a list of all imports required.
    Args: script filenames.
    Returns: list of imports
    script_nodes = []
    scripts = set(map(os.path.abspath, scripts))

    # Process the scripts and build the map of imports
    graph = initialize_modgraph()
    for script in scripts:
    for node in graph.nodes():
        if node.filename in scripts:

    # Search the imports to find what is in use
    dependency_nodes = set()
    def search_dependencies(node):
        for reference in graph.getReferences(node):
            if reference not in dependency_nodes:
    for script_node in script_nodes:

    return list(sorted(dependency_nodes))

if __name__ == '__main__':
    # Show the PyInstaller imports used in this file
    for node in get_import_dependencies(__file__):
        if node.identifier.split('.')[0] == 'PyInstaller':

All the node types are defined in PyInstaller.lib.modulegraph.modulegraph, such as SourceModule, MissingModule, Package and BuiltinModule. These will come in useful when performing checks.

Each of these has an identifier (, and depending on the node type, it may have a filename (C:/path/to/my/module/, and packagepath (['C:/path/to/my/module']).

I can’t really post any extra code as it is quite specific to our setup with using pyarmor with PyInstaller, I can happily say it works flawlessly so far though.

Answered By: stemboy400

Leave a Reply

Your email address will not be published. Required fields are marked *