Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug pip install <package name>

If I want to debug the setup.py file of my package packagename getting installed via "pip install packagename" , is there a way to do so? I have tried downloading the source, adding set_trace() in setup.py and run:

    pip install .

However, as soon I get pdb() prompt, the install fails with error:

processing /Users/skauser/python-ibmdb/IBM_DB/ibm_db
    Complete output from command python setup.py egg_info:
    > /private/var/folders/b6/pmddncpn77550p8_g9kkx9f40000gp/T/pip-req-build-_fg8s5a2/setup.py(31)<module>()
    -> machine_bits =  8 * struct.calcsize("P")
    (Pdb)
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/var/folders/b6/pmddncpn77550p8_g9kkx9f40000gp/T/pip-req-build-_fg8s5a2/setup.py", line 31, in <module>
        machine_bits =  8 * struct.calcsize("P")
      File "/private/var/folders/b6/pmddncpn77550p8_g9kkx9f40000gp/T/pip-req-build-_fg8s5a2/setup.py", line 31, in <module>
        machine_bits =  8 * struct.calcsize("P")
      File "/Library/anaconda3/lib/python3.7/bdb.py", line 88, in trace_dispatch
        return self.dispatch_line(frame)
      File "/Library/anaconda3/lib/python3.7/bdb.py", line 113, in dispatch_line
        if self.quitting: raise BdbQuit
    bdb.BdbQuit

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/b6/pmddncpn77550p8_g9kkx9f40000gp/T/pip-req-build-_fg8s5a2/

Although I can debug the source via: python setup.py build, the behavior that I want to debug is applicable when installed through pip.

like image 387
Saba Kauser Avatar asked Apr 20 '19 03:04

Saba Kauser


People also ask

How do I debug a Python package?

Starting Python Debugger To start debugging within the program just insert import pdb, pdb. set_trace() commands. Run your script normally, and execution will stop where we have introduced a breakpoint. So basically we are hard coding a breakpoint on a line below where we call set_trace().

How do I specify package version in pip install?

To install a specific version of a Python package you can use pip: pip install YourPackage==YourVersion . For example, if you want to install an older version of Pandas you can do as follows: pip install pandas==1.1. 3 .

What is pip installable?

The pip install <package> command always looks for the latest version of the package and installs it. It also searches for dependencies listed in the package metadata and installs them to ensure that the package has all the requirements that it needs.

What is the flag for pip install?

The --user flag to pip install tells Pip to install packages in some specific directories within your home directory. This is a good way to have your own default Python environment that adds to the packages within your system directories, and therefore, does not affect the system Python installation.


1 Answers

Pip is a python program. You can see it's content with cat $(which pip). Then you can copy it to a new file in your project's directory. For example, here is how it looks like for me:

File mypip.py:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
import sys
from pip._internal.cli.main import main

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(main())

Then you can add a breakpoint with pdb or use PyCharm as mentioned in the answer from Igor Yudnikov.

For reference, this is how it to looks like in my Pycharm enter image description here

like image 100
jozo Avatar answered Sep 28 '22 13:09

jozo