Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Linux: How to find the location of a specific "include" library in Python?

Note:
I have already searched extensively and found nothing that even vaguely resembles an answer, including here. If this is a stupid question or a duplicate, please accept my apologies.

Also Note:
Please don’t advise me to update [X] as it cannot be done.

The operating system version and the version of Python installed, (etc.), are hard requirements and cannot be changed without a non-trivial effort which isn’t going to happen. Correction: There are ongoing efforts to update the gopigo libraries to run on Bullseye and Bookworm, but these are long-term projects that won’t help me here as they’re not even close to completion.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Situation:
I have a robot running Raspberry Pi O/S Buster with Python 3.7 installed.

Many of the robot’s programming interfaces are Python files that include various libraries. For example, two libraries are universally used: "easygopigo" and "gopigo".

For whatever reason, there seem to be six or seven copies of these files scattered around, some in the "pi" user’s home directory and/or subdirectories, others in various other places within the filesystem.

Question:
Within a particular Python program, how do I know what and where a particular included file is?

For example: Given "import EASY from easygopigo", how do I find which one of many "easygopigo" libraries are being used?

In other words, I’d like the Python equivalent of "which [command]" as in "which [library file]", that would give me the path and filename of the file that would be used when I include it.

Pardon my stupidity and thanks in advance for any help you can provide.

>Solution :

after it has been imported, callstr() on it, or (for many/most modules) look at the .__path__ attribute of the module.

>>> import numpy
>>> print(str(numpy))
<module 'numpy' from '/Library/Frameworks/Python.framework/Versions/3.12/...numpy/__init__.py'
>>> numpy.__path__
['/Library/Frameworks/Python.framework/Versions/3.12/.../site-packages/numpy']

To determine before it is actually imported, you need to search through sys.path

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading