Python: How to list imported modules [duplicate] - python

This question already has answers here:
How to list imported modules?
(10 answers)
Closed 3 years ago.
In Python calling the import function brings in a module to give a program access to its namespace of classes, functions and variables.
import os
import sys
print(os.name)
print(sys.implementation)
Is there a way to list which modules have already been imported in a program?
>>> list_imports()
sys
os
And if so, is there a way to figure out the version of the imported module?

sys.modules is the closest you'll get. Modules following spec will have a __version__ attribute.

Just use sys library
import sys
sys.modules.keys()

Related

How does Python import a module of big Github project? [duplicate]

This question already has answers here:
What's the difference between a Python module and a Python package?
(9 answers)
Closed 1 year ago.
I can't understand how the python import works for big projects on the Github.
Everyone knows the import statement - "from packageName import moduleName".
But for some big projects, for example Django.
I've got tutored "from django.urls import path". (https://docs.djangoproject.com/en/3.2/topics/http/urls/)
But couldn't find any path.py file under /django/urls directory from its Github structure. (https://github.com/django/django/tree/main/django/urls)
Did I miss any advanced import mechanism?
If you look at __init__.py, it imports the name path from conf.py. This makes path available as a variable in the django.urls module which can be imported.

Find path of a package (e.g. pandas) in Python [duplicate]

This question already has answers here:
How to retrieve a module's path?
(23 answers)
Closed 2 years ago.
Is there a way to programmatically find the location of a python package once it is imported?
For example:
import os
import pandas
os.get_package_directory(panda)
>>> C:\Users\username\miniconda3\pkgs\pandas-1.0.5-py37h47e9c7a_0
It's been a while since I've done this, but try this:
import pandas
print(pandas.__file__)

How to import files/modules from other directory in python? [duplicate]

This question already has answers here:
How can I import a module dynamically given the full path?
(35 answers)
Closed 3 years ago.
I want to import some files/modules from other directory.
I have placed an init.py in the directory but still not able to import the files/modules.
I am using Pycharm 2019.
I want to use init.py only method and not the sys.path.append.
thanks in Advance!
there are multiple ways to do that, I'll name two -
import sys
sys.path.insert(0, 'path/to/your/py_file')
import py_file
is relatively easy, and another one is if you're using pycharm just mark the directory of which you want to import from as a sources root.

The dot in module import (python 3.x) [duplicate]

This question already has answers here:
What does a . in an import statement in Python mean?
(3 answers)
Closed 4 years ago.
For what i can use the "." in import module. I meet it in sklearn library. It looks like:
from .externals import six
The . is a shortcut that tells it search in current package before rest of the PYTHONPATH. So, if a same-named module Recipe exists somewhere else in your PYTHONPATH, it won't be loaded.

Import modules using an alias [duplicate]

This question already has answers here:
Python: importing a sub‑package or sub‑module
(3 answers)
Closed 6 years ago.
When attempting to import from an alias - which is common in scala I was surprised to see the following results:
Create an alias
import numpy as np
Use the alias to import modules it contains
from np import linalg
ImportError: No module named np.linalg
Is there any other syntax/equivalent in python useful for importing modules?
Using import module as name does not create an alias. You misunderstood the import system.
Importing does two things:
Load the module into memory and store the result in sys.modules. This is done once only; subsequent imports re-use the already loaded module object.
Bind one or more names in your current namespace.
The as name syntax lets you control the name in the last step.
For the from module import name syntax, you need to still name the full module, as module is looked up in sys.modules. If you really want to have an alias for this, you would have to add extra references there:
import numpy # loads sys.modules['numpy']
import sys
sys.modules['np'] = numpy # creates another reference
However, doing so can have side effects when you also are importing submodules. Generally speaking, you don't want to create aliases for packages by poking about in sys.modules without also creating aliases for all (possible) submodules as not doing so can cause Python to re-import submodules as separate namespaces.
In this specific case, importing numpy also triggers the loading of numpy.linalg, so all you really have to do is:
import numpy as np
# np.linalg now is available
No module aliasing is needed. For packages that don't import submodules automatically, you'd have to use:
import package as alias
import package.submodule
and alias.submodule is then available anyway, because a submodule is always added as an attribute on the parent package.
My understanding of your example would be that since you already imported numpy, you couldn't re import it with an alias, as it would already have the linalg portion imported.

Categories