Why can't list the pandas as dependencies?

There is only one file in /usr/lib/python3.9/dist-packages/mytest:

tree  /usr/lib/python3.9/dist-packages/mytest
/usr/lib/python3.9/dist-packages/mytest
└── mytest.py

0 directories, 1 file

Show content in it:

cat  /usr/lib/python3.9/dist-packages/mytest/mytest.py
import  pandas
print('i am testing')

I want to list all dependencies in mytest directory:

pip list /usr/lib/python3.9/dist-packages/mytest  | grep pandas
#nothing as output

Why can’t list the pandas as dependencies for the package mytest?

The pip list command lists the packages that are installed in an environment. It does not list the packages that a certain script requires.

In Python, imports are dynamic, so it is not even possible to get the dependencies of an arbitrary Python file if they are not declared somehow.

2 Likes

Is there a way to list such dependencies by some other command or by other arrangement?

You need to tell Python what the dependencies are - there’s no way that Pip or any other tool can tell you, because the source code doesn’t actually have enough information. If you write import pandas then it could just as easily mean that you wanted to use the code in pandas.py (or in a pandas folder) from your own project. Even if we know that the dependency should come from installing something from PyPI, the name in your code doesn’t have to match the name that Pip and PyPI use. For example, we might want to pip install opencv-python in order to import cv2, or pip install Pillow in order to import PIL. There’s no way to predict that from the source code - you have to just know.

If you use pip freeze it will output a list of what has already been installed, and you can save that and reuse it to set up another copy of Python (or a virtual environment) with the same libraries.

If you are writing your own code and you want to figure out what you need to install in order to make it run, you need to do research. If you wrote import pandas because you want to use Pandas, then that should mean that you already know there is a Pandas website with documentation - and it will tell you what to install and how.

If you are trying to make someone else’s code run and you need to install something else first, it was the code author’s responsibility to fix that for you. When you see a project on Github it should have a pyproject.toml and/or setup.py file that is used for installing the project, which also means installing any libraries that it needs. Major projects on Github should also show a README or other documentation that explains how to use the code. (If you find something written by just some random person and you can’t figure out how to use it, it probably isn’t really meant for other people to use yet.)