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

Asked

Viewed 2,868 times

2

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

Everything works perfectly, but the installation got too big because of the libraries, as I have many libraries that I use in other projects, and that has nothing to do with this installation, got too big, I tried to choose what I use and what I don’t use by looking at the Imports in the project, but turn and stir during the tests there are some errors because of a library that is missing, and not always the name is suggestive to look for, at the end of the day I had to leave all the libraries afraid to give some error in production.

I can somehow figure out which libraries are being used in a particular project?

2 answers

3


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

$ pip freeze > requirements.txt

In the production environment, do:

$ pip install -r requirements.txt

Because although quick and easy, this form can be considered "careless"? because that way you will be installing in production all the packages installed in the development environment, this, most of the time, unnecessary.

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

A good practice is to always start each project in a unique virtual environment, for this the ideal is to use a venvs manager, what I like most is the anacoda/Conda which, besides being a venv manager is also a python package manager, a kind of "distribution".

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

pip install -r requirements.txt

Pipreqs:

If you didn’t do any of this and want to avoid the 'careless' way of solving the problem, let’s try to do a kind of "reverse engineering" with pipreqs (although not yet the ideal form, but very close to it). With pipreqs you build your Requirements.txt based on your Mports, but automatically and not in the Olhometro, to do this just 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 works with Requirements.txt in your project directory, now simply include it in your installer and run pip install -r reqirements at the facility.

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

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 (in the correct or "careless" way) you will no longer need to "load" the library folder in the installation.

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

  • Friend let me ask you, I’m in windows, and I’m trying to run pipreqs, I tried this way: python C: Python27 Lib site-Packages pipreqs pipreqs.py and it presents the following error: Import: cannot import name version

  • You don’t have Pip installed in windows? if you don’t have it, install it and use it to install the packages. That link can help you install Pip on windows.

  • I already installed pipreqs using Pip, but while trying to run from this error.

  • Dude... I haven’t been in touch with windows for a while, but theoretically it would have to be like linux, just give the command pipreqs c:\path\projeto. See if pipreqs is a binary (executable in windows), with the whereis command, if I’m not mistaken in windows is where, try like this where pipreqs, if it is see if its path is in the path variable.

  • In time: Consider using anaconda and end your package problems and their conflicts.

0

=help("modules")

Use the native function HELP. In the Python prompt menu type: help("modules") and the program will return the list of all modules installed in alphabetical order.

Remarks:

1) The first time can take a while;

2) The list is very complete and shows internal modules of the main modules and pre-installed modules.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.