-2

in a python file, how to check the version of all the libraries that are imported exactly in that file by "import xxxxxxxxxxxx".

This is useful to later make an environment that has enough library to run that script while don't need to clone the exact environment.

example:

import pandas
import tensorflow

def version_list():
  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

# expect the version_list to return  
# pandas==xxx tensorflow==xxx     
# without returning other things
Luk Aron
  • 1,235
  • 11
  • 34
  • I think you are approaching it the wrong way. Why not do `pip freeze > requirements.txt` to get all the versions of your installed packages in your current environment. That way, when you "*later make an environment*", you can re-install all those exact same versions. – Gino Mempin Aug 06 '21 at 00:28
  • Does this answer your question? [How to check version of python modules?](https://stackoverflow.com/questions/20180543/how-to-check-version-of-python-modules) – Gino Mempin Aug 06 '21 at 00:33
  • @GinoMempin "This is useful to later make an environment that has enough library to run that script while __don't need to clone the exact environment.__" – Luk Aron Aug 06 '21 at 00:47
  • The duplicate link has answers that mention the `__version__` attribute, **same as the answer you just accepted**. It's also asking the same thing: "_**I want to be able to (programmatically) check their versions**_" similar to your `import xxx`. I fail to see how they are different. – Gino Mempin Aug 06 '21 at 00:58
  • the answer is accepted by misclicking i am sorry for that – Luk Aron Aug 06 '21 at 01:28

1 Answers1

-1

Many modules have their version set to the variable __version__. For example

import numpy
print(numpy.__version__)

prints the version of numpy which you have imported.

This may be related: Standard way to embed version into Python package?

Scene
  • 489
  • 4
  • 16