How to know which libraries are being used in a python project?

2

I made an installation of an application made in Python, it basically works as follows, it installs the Python program and in the end the installation copies the folder with the libraries for the Python installation.

Everything works perfectly, but the installation was very large because of the libraries, as I have many libraries that I use in other projects, and that has nothing to do with this installation, it was very large, I tried to choose what I use and which I do not use looking at the imports in the project, but turn and move during the tests happen some errors because of a library that is missing, and not always the name is suggestive to look for, in the end I had to leave all the libraries with fear of making some mistake in production.

I can somehow know which libraries are being used in a given project?

    
asked by anonymous 02.10.2017 / 20:26

1 answer

3

Quickly and carelessly you can "fix" by doing so:
In the environment of your project do:

$ pip freeze > requirements.txt

In the production environment, do:

$ pip install -r requirements.txt

Because it is fast and easy, this form can be considered "careless"? because in this way you will be installing all packages installed in the development environment in production, this is often unnecessary.

But if you want to do it right, keep reading.

A good practice is to always start each project in a single virtual environment, so it is best to use a venvs manager, what I like most is anacoda / conda, which, besides being a venv's manager, a python package manager, a kind of "distribution."

If you had used this practice, then at the beginning of the project development you would create an env only with the version of python that you would use in the project and as you needed the libs would feed a file called requeriments.txt, so when it was to the production environment, it would be enough to do:

pip install -r requirements.txt

Pipreqs:

If you have not done any of this and want to avoid the 'careless' way of solving the problem, let's try to do some sort of "reverse engineering" with pipreqs (although not yet the ideal form, . With pipreqs vc build your requirements.txt based on your imports, but automatically and not in the olhometro, for this would suffice to do:

# Instalando o pipreqs
$ pip install pipreqs

# Construindo o requirements.txt
$ pipreqs /path/projeto
INFO: Successfully saved requirements file in /path/projeto/requirements.txt

Pireqs will create the requirements.txt in the directory of your project, now just include it in your installer and run pip install -r reqirements in the installation.

Why did I say that is not ideal in this way? In the development environment (even considering a single virtual environment) you may want to install some packages that are not needed in the production environment, so it's always best to work as virtualenvs, as "lean" as possible, and manually add libs requeriments.txt as you develop.

Pipreqs has some interesting parameters, see:

$ pipreqs --help
pipreqs - Generate pip requirements.txt file based on imports

Usage:
    pipreqs [options] <path>

Options:
    --use-local           Use ONLY local package info instead of querying PyPI
    --pypi-server <url>   Use custom PyPi server
    --proxy <url>         Use Proxy, parameter will be passed to requests library. You can also just set the
                          environments parameter in your terminal:
                          $ export HTTP_PROXY="http://10.10.1.10:3128"
                          $ export HTTPS_PROXY="https://10.10.1.10:1080"
    --debug               Print debug information
    --ignore <dirs>...    Ignore extra directories, each separated by a comma
    --encoding <charset>  Use encoding parameter for file open
    --savepath <file>     Save the list of requirements in the given file
    --print               Output the list of requirements in the standard output
    --force               Overwrite existing requirements.txt

Obs. In both cases (the correct way or the "careless" form) you will no longer have to "load" the folder of the libraries in the installation.

To learn more about anaconda, see this answer here at STOpt.

    
02.10.2017 / 22:39