Exemples de comment obtenir une liste des modules python disponibles ou utilisés dans un script pour créer un fichier requirements.txt ?
Obtenir une liste des modules python disponibles
Pour obtenir une liste des modules python disponibles, une solution est d'utiliser:
pip freeze
donne par exemple:
absl-py==0.9.0alabaster==0.7.10anaconda-client==1.6.9anaconda-navigator==1.7.0anaconda-project==0.8.2appnope==0.1.0appscript==1.0.1asn1crypto==0.24.0astor==0.8.1astroid==1.6.1astropy==2.0.3attrs==17.4.0Babel==2.5.3backports.shutil-get-terminal-size==1.0.0beautifulsoup4==4.6.0bitarray==0.8.1bkcharts==0.2blaze==0.11.3bleach==2.1.2bokeh==0.12.13boto==2.48.0Bottleneck==1.2.1cachetools==3.1.1certifi==2018.1.18cffi==1.11.4chardet==3.0.4click==6.7cloudpickle==0.5.2clyent==1.2.2colorama==0.3.9conda==4.8.3conda-build==3.4.1conda-package-handling==1.7.0+0.g7c4a471.dirtyconda-verify==2.0.0contextlib2==0.5.5cryptography==2.1.4cycler==0.10.0Cython==0.27.3cytoolz==0.9.0dask==0.16.1datashape==0.5.4decorator==4.2.1distributed==1.20.2docutils==0.14entrypoints==0.2.3et-xmlfile==1.0.1fastcache==1.0.2filelock==2.0.13Flask==0.12.2Flask-Cors==3.0.3geoip2==2.9.0gevent==1.2.2glob2==0.6gmpy2==2.0.8google-api-python-client==1.7.11google-auth==1.6.3google-auth-httplib2==0.0.3greenlet==0.4.12h5py==2.7.1heapdict==1.0.0html5lib==1.0.1httplib2==0.14.0idna==2.6imageio==2.2.0imagesize==0.7.1ipykernel==4.8.0ipython==6.2.1ipython-genutils==0.2.0ipywidgets==7.1.1isort==4.2.15itsdangerous==0.24jdcal==1.3jedi==0.11.1Jinja2==2.10jsonschema==2.6.0jupyter==1.0.0jupyter-client==5.2.2jupyter-console==5.2.0jupyter-core==4.4.0jupyterlab==0.31.5jupyterlab-launcher==0.10.2lazy-object-proxy==1.3.1llvmlite==0.21.0locket==0.2.0lxml==4.1.1MarkupSafe==1.0matplotlib==2.1.2maxminddb==1.4.1mccabe==0.6.1mistune==0.8.3mpmath==1.0.0msgpack-python==0.5.1multipledispatch==0.4.9navigator-updater==0.1.0nbconvert==5.3.1nbformat==4.4.0networkx==2.1nltk==3.2.5nose==1.3.7notebook==5.4.0numba==0.36.2numexpr==2.6.4numpy==1.14.0numpydoc==0.7.0oauth2client==4.1.3odo==0.5.1olefile==0.45.1openpyxl==2.4.10packaging==16.8pandas==0.22.0pandocfilters==1.4.2parso==0.1.1partd==0.3.8path.py==10.5pathlib2==2.3.0patsy==0.5.0pdf2image @ file:///home/conda/feedstock_root/build_artifacts/pdf2image_1588278609827/workpep8==1.7.1pexpect==4.3.1pickleshare==0.7.4Pillow==5.0.0pkginfo==1.4.1pluggy==0.6.0ply==3.10prompt-toolkit==1.0.15protobuf==3.12.4psutil==5.4.3ptyprocess==0.5.2py==1.5.2pyasn1==0.4.7pyasn1-modules==0.2.6pycodestyle==2.3.1pycosat==0.6.3pycparser==2.18pycrypto==2.6.1pycurl==7.43.0.1pyflakes==1.6.0Pygments==2.2.0pylint==1.8.2pyodbc==4.0.22pyOpenSSL==17.5.0pyparsing==2.2.0PySocks==1.6.7pytest==3.3.2python-dateutil==2.6.1pytz==2017.3PyWavelets==0.5.2PyYAML==3.12pyzmq==16.0.3QtAwesome==0.4.4qtconsole==4.3.1QtPy==1.3.1requests==2.18.4rope==0.10.7rsa==4.0ruamel-yaml==0.15.35scikit-image==0.13.1scikit-learn==0.19.1scipy==1.0.0seaborn==0.8.1Send2Trash==1.4.2simplegeneric==0.8.1singledispatch==3.4.0.3six==1.12.0snowballstemmer==1.2.1sortedcollections==0.5.3sortedcontainers==1.5.9Sphinx==1.6.6sphinxcontrib-websupport==1.0.1spyder==3.2.6SQLAlchemy==1.2.1statsmodels==0.8.0sympy==1.1.1tables==3.4.2tblib==1.3.2tensorflow-docs @ git+https://github.com/tensorflow/docs@cb886cfdd16d66ff7f8d1430676ff395b02910e6terminado==0.8.1testpath==0.3.1toolz==0.9.0tornado==4.5.3tqdm==4.46.0traitlets==4.3.2typing==3.6.2unicodecsv==0.14.1uritemplate==3.0.0urllib3==1.22wcwidth==0.1.7webencodings==0.5.1Werkzeug==0.14.1widgetsnbextension==3.1.0wrapt==1.10.11xlrd==1.1.0XlsxWriter==1.0.2xlwings==0.11.5xlwt==1.2.0zict==0.1.3
Pour créer un fichier requirements.txt avec pip freeze on peut alors faire comme ceci:
pip freeze > requirements.txt
Modules utilisés dans un script python
Pour trouver les modules python utilisés dans un script python (par exemple test.py), une solution est d'utiliser ModuleFinder:
from modulefinder import ModuleFinderfinder = ModuleFinder()finder.run_script('test.py')print('Loaded modules:')for name, mod in finder.modules.items():print('%s: ' % name, end='')print(','.join(list(mod.globalnames.keys())[:3]))print('-'*50)print('Modules not imported:')print('\n'.join(finder.badmodules.keys()))
Note: cette option semble ne par marcher avec anaconda (j'ai obtenu une liste de tous les modules plutot que les modules utilisés dans le script).
Modules dans un jupyter notebook
Dans un jupyter notebook on peut faire comme ceci
import typesdef imports():for name, val in globals().items():if isinstance(val, types.ModuleType):yield val.__name__list(imports())
donne par exemple
['builtins','builtins','types','tensorflow','tensorflow_docs','tensorflow_docs','matplotlib.pyplot','matplotlib','matplotlib.cm','pandas','numpy','numpy.ma','seaborn','os','warnings','tensorflow_core.keras','tensorflow_core.keras.layers']
Note:
!pip freeze
donne la liste de tous les modules pouvant être importés dans le jupyter notebook:
absl-py==0.9.0alabaster==0.7.10anaconda-client==1.6.9anaconda-navigator==1.7.0anaconda-project==0.8.2appnope==0.1.0appscript==1.0.1asn1crypto==0.24.0astor==0.8.1astroid==1.6.1astropy==2.0.3attrs==17.4.0Babel==2.5.3backports.shutil-get-terminal-size==1.0.0beautifulsoup4==4.6.0bitarray==0.8.1bkcharts==0.2

