What is so special about ldap.modlist that I have to explicitly import it? Importing ldap is not enough:
>>> import ldap
>>> ldap.modlist
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'modlist'
But if I explicitly import it, then all is well:
>>> import ldap
>>> import ldap.modlist
>>> ldap.modlist
<module 'ldap.modlist' from '/opt/rh/python27/root/usr/lib64/python2.7/site-packages/ldap/modlist.pyc'>
Why? Other modules don't behave like this:
>>> import os
>>> os.path
<module 'posixpath' from '/opt/rh/python27/root/usr/lib64/python2.7/posixpath.pyc'>
>>> import ldap
>>> ldap.dn
<module 'ldap.dn' from '/opt/rh/python27/root/usr/lib64/python2.7/site-packages/ldap/dn.pyc'>
The difference between the modules ldap.dn and ldap.modlist is this:
The module ldap.dn is loaded during the initialization of the ldap package and is therefore already present as an attribute in the ldap package (see The Import System: Submodules for a description about submodules):
# ldap/__init__.py, line 89 of python-ldap version 2.4.27
from ldap.dn import explode_dn,explode_rdn,str2dn,dn2str
The module ldap.modlist on the other hand is not loaded during the initalization of the ldap package.
Related
I'm able to import a module from a directory with importlib, but I can't seem to utilize the classes correctly.
Without importlib, I can import the class as follows:
>>> from PySide2.QtWidgets import QApplication
>>> app = QApplication()
>>> print(app)
<PySide2.QtWidgets.QApplication object at 0x7fe3af6a9080>
But when I try to import it using importlib (which is needed for my dynamic imports), I get the following:
>>> import sys
>>> import importlib.util
>>> package_path = '/var/tmp/PySide2/__init__.py'
>>> spec = importlib.util.spec_from_file_location('PySide2.QtWidgets.QApplication', package_path)
>>> QApplication = importlib.util.module_from_spec(spec)
>>> sys.modules[spec.name] = QApplication
>>> spec.loader.exec_module(QApplication)
>>>
>>> app = QApplication()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
How can I get QApplication to behave the same as it does with from PySide2.QtWidgets import QApplication?
I tried some things with importlib.import_module and getattr(module, class), but I haven't been able to understand how to use import_module to import a module from a specific path in the filesystem.
I'm using Python 3.5
EDIT
I've also tried the following:
>>> package_path = '/var/tmp/PySide2/__init__.py'
>>> spec = importlib.util.spec_from_file_location('PySide2', package_path)
>>> PySide2 = importlib.util.module_from_spec(spec)
>>> sys.modules[spec.name] = PySide2
>>> spec.loader.exec_module(PySide2)
>>>
>>> app = PySide2.QtWidgets.QApplication()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'PySide2' has no attribute 'QtWidgets'
and
>>> QtWidgets = getattr(PySide2, 'QtWidgets')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'PySide2' has no attribute 'QtWidgets'
EDIT 2
Here are the top-level contents of the PySide2 directory, which was installed via python3 -m pip install PySide2==5.12.3, and the resulting PySide2 directory was then copied to /var/tmp/
_config.py Qt3DRender.pyi QtPositioning.pyi QtUiTools.pyi
examples QtCharts.abi3.so QtPrintSupport.abi3.so QtWebChannel.abi3.so
_git_pyside_version.py QtCharts.pyi QtPrintSupport.pyi QtWebChannel.pyi
glue QtConcurrent.abi3.so QtQml.abi3.so QtWebEngine.abi3.so
include QtConcurrent.pyi QtQml.pyi QtWebEngineCore.abi3.so
__init__.py QtCore.abi3.so QtQuick.abi3.so QtWebEngineCore.pyi
libpyside2.abi3.so.5.12 QtCore.pyi QtQuick.pyi QtWebEngine.pyi
__pycache__ QtDataVisualization.abi3.so QtQuickWidgets.abi3.so QtWebEngineWidgets.abi3.so
PySide QtDataVisualization.pyi QtQuickWidgets.pyi QtWebEngineWidgets.pyi
PySide2 QtGui.abi3.so QtRemoteObjects.abi3.so QtWebSockets.abi3.so
pyside2-lupdate QtGui.pyi QtRemoteObjects.pyi QtWebSockets.pyi
pyside2-rcc QtHelp.abi3.so QtScxml.abi3.so QtWidgets.abi3.so
Qt QtHelp.pyi QtScxml.pyi QtWidgets.pyi
Qt3DAnimation.abi3.so QtLocation.abi3.so QtSensors.abi3.so QtX11Extras.abi3.so
Qt3DAnimation.pyi QtLocation.pyi QtSensors.pyi QtX11Extras.pyi
Qt3DCore.abi3.so QtMultimedia.abi3.so QtSql.abi3.so QtXml.abi3.so
Qt3DCore.pyi QtMultimedia.pyi QtSql.pyi QtXmlPatterns.abi3.so
Qt3DExtras.abi3.so QtMultimediaWidgets.abi3.so QtSvg.abi3.so QtXmlPatterns.pyi
Qt3DExtras.pyi QtMultimediaWidgets.pyi QtSvg.pyi QtXml.pyi
Qt3DInput.abi3.so QtNetwork.abi3.so QtTest.abi3.so scripts
Qt3DInput.pyi QtNetwork.pyi QtTest.pyi support
Qt3DLogic.abi3.so QtOpenGL.abi3.so QtTextToSpeech.abi3.so typesystems
Qt3DLogic.pyi QtOpenGL.pyi QtTextToSpeech.pyi
Qt3DRender.abi3.so QtPositioning.abi3.so QtUiTools.abi3.so
I think you're messing up the definition of modules. While from PySide2.QtWidgets is a module (file), QApplication is a definition inside of this module (a class definition).
To use import_module, you must give the path to the module. This will return a module object (as in import PySide2.QtWidgets as module). You can then use the module as you wish :)
Example:
import importlib
module = importlib.import_module('PySide2.QtWidgets')
QApplication = module.QApplication
app = QApplication()
Definition of package
Definition of module
Definition of importing
To import from a specific location, you can do almost as you did in the question (Importing a source file directly):
import importlib.util
# Note that I'm using the module path, not the package path
module_path = '/var/tmp/PySide2/QtWidgets.py'
spec = importlib.util.spec_from_file_location('PySide2.QtWidgets', module_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
QApplication = module.QApplication
app = QApplication()
I'm trying to do a dynamic import (using "__import__()") of a submodule in web2py and it doesn't seem to be finding the submodule.
Here's an example path:
web2py/app/modules/module.py <-- This works
web2py/app/modules/module_folder/submodule.py <-- This won't get spotted.
Right now as a workaround I'm using 'exec()' but I'd rather not do that.
Answers to questions:
"Do you have __init__.py in module_folder?"
Yep.
"What exactly is the line of code you write to import web2py/app/modules/module_folder/submodule.py?"
mapping_module = __import__('communication_templates.mappings.%s' % module_name)
"what is the error?"
<type 'exceptions.AttributeError'> 'module' object has no attribute 'mapping_dict'
Explanation: I'm trying to get a variable named 'mapping_dict' from the module once I've loaded it.
The problem here is that __import__ does not do the most intuitive thing (neither does import btw).
Here's what happens:
>>> import sys
>>> x = __import__('package1.package2.module')
>>> x
<module 'package1' from 'C:\\Temp\\package1\\__init__.py'>
Even though package1.package2.module was imported, the variable returned is actually package1.
So, to access something that is in package1.package2.module, one has to dig down again:
>>> x.package2.module.Class
<class 'package1.package2.module.Class'>
Is it then the same as importing just package1?
Not really:
>>> x = __import__('package1')
>>> x
<module 'package1' from 'C:\\Temp\\package1\\__init__.py'>
>>> x.package2.module.Class
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'package1' has no attribute 'package2'
Why?
Well, __import__ is the same as `import, that is, these are the same:
package1 = __import__('package1.package2.module')
# is the same as:
import package1.package2.module
And:
package1 = __import__('package1')
# is the same as:
import package1
In each of those cases, you get just one local variable package1.
In the case of importing package1.package2.module, package2 is also imported and stored in package2 attribute of package package1 etc.
Solution
To access package1.package2.module.Class, if package1.package2.module is in a string:
>>> s = 'package1.package2.module'
>>> module = __import__(s)
>>> for submodule_name in s.split('.')[1:]:
... module = getattr(module, submodule_name)
...
>>> module.Class
<class 'package1.package2.module.Class'>
I'm trying to use the importlib library to verify whether the nmap library is installed on the computer executing the script in Python 3.5.2
I'm trying to use importlib.util.find_spec("nmap") but receive the following error.
>>> import importlib
>>> importlib.util.find_spec("nmap")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'importlib' has no attribute 'util'
Can someone tell me where I'm going wrong?
EDIT
I was able to get the function to work using the following code.
#!/usr/bin/pythonw
import importlib
from importlib import util
#check to see if nmap module is installed
find_nmap = util.find_spec("nmap")
if find_nmap is None:
print("Error")
Try this:
from importlib import util
util.find_spec("nmap")
I intend to investigate, but honestly I don't know why one works and the other doesn't. Also, observe the following interactive session:
>>> import importlib
>>> importlib.util
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'importlib' has no attribute 'util'
>>> from importlib import util
>>> util
<module 'importlib.util' from '/usr/lib/python3.5/importlib/util.py'>
>>> importlib.util
<module 'importlib.util' from '/usr/lib/python3.5/importlib/util.py'>
So...yeah. I am sure this makes perfect sense to someone, but not to me. I will update once I figure it out.
Update:
Comparing this to something like:
>>> import datetime
>>> datetime
<module 'datetime' from '/usr/lib/python3.5/datetime.py'>
>>> datetime.datetime
<class 'datetime.datetime'>
I think the difference is that in this case the first datetime is a module and the second is a class, while in the importlib.util case both are modules. So perhaps module.module is not OK unless the code from both modules has been loaded, while module.class is OK, because the class code is loaded when the module is imported.
Update #2
Nope, it seems like in many cases module.module is fine. For example:
>>> import urllib
>>> urllib
<module 'urllib' from '/usr/lib/python3.5/urllib/__init__.py'>
>>> urllib.error
<module 'urllib.error' from '/usr/lib/python3.5/urllib/error.py'>
So perhaps it is something specific to importlib.
Update #3
As #kfb pointed out in the comments, it does seem to be related to importlib specifically. See the following comment from the __init__.py for importlib:
# Until bootstrapping is complete, DO NOT import any modules that attempt
# to import importlib._bootstrap (directly or indirectly). Since this
# partially initialised package would be present in sys.modules, those
# modules would get an uninitialised copy of the source version, instead
# of a fully initialised version (either the frozen one or the one
# initialised below if the frozen one is not available).
importlib/util.py does import importlib._bootstrap so I would assume that this is realted. If my understanding is correct, when you do import importlib the submodules will be initialized, but are not initialized for the importlib module object that you have imported. At this point, if you do dir(importlib) you will not see util. Interestingly, after you have tried to access importlib.util and gotten an AttributeError, util (along with the other submodules) gets loaded/initialized, and now you can access importlib.util!
>>> import importlib
>>> dir(importlib)
['_RELOADING', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__import__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_bootstrap', '_bootstrap_external', '_imp', '_r_long', '_w_long', 'find_loader', 'import_module', 'invalidate_caches', 'reload', 'sys', 'types', 'warnings']
>>> importlib.util
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'importlib' has no attribute 'util'
>>> importlib.util
<module 'importlib.util' from '/usr/lib/python3.5/importlib/util.py'>
>>> dir(importlib)
['_RELOADING', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__import__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_bootstrap', '_bootstrap_external', '_imp', '_r_long', '_w_long', 'abc', 'find_loader', 'import_module', 'invalidate_caches', 'machinery', 'reload', 'sys', 'types', 'util', 'warnings']
I have a package named 'package'within that i have modules module1.py and module2.py i imported the package as
import package
from package import module1
In module1 i have a function named funcwhenever i import that function as
from module1 import func
and use it, the function as
module1.func(x)
it doesn't work
What is the problem and what should be done??
You can do either:
from module1 import func
func(x)
OR
module1.func(x)
Real world example which should demonstrate how things work:
>>> import os
>>> os.path.abspath("C:/Documents")
'C:\\Documents'
>>>
>>> from os import path
>>> path.abspath("C:/documents")
'C:\\documents'
>>>
>>> from path import abspath
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named path
>>>
>>> from os.path import abspath
>>> abspath("C:/documents")
'C:\\documents'
You can either import as:
from foo import bar
bar(baz)
or:
import foo
foo.bar(baz)
In certain cases, it may also be helpful to:
from foo import bar as qux
qux(baz
There is an extensive tutorial on handling imports available as well.
2 options:
from package.module1 import func
func(x)
2nd option:
from package import module1
module1.func(x)
I want to get the path and source code of the __builtin__ module, where can I get it?
Latest (trunk) C sources of __builtin__ module: http://svn.python.org/view/python/trunk/Python/bltinmodule.c?view=markup
The __builtin__ module is built-in, there is no Python source for it. It's coded in C and included as part of the Python interpreter executable.
You can't. it is built-in to the interpreter.
>>> # os is from '/usr/lib/python2.7/os.pyc'
>>> import os
>>> os
<module 'os' from '/usr/lib/python2.7/os.pyc'>
>>> # PyQt4 is from '/usr/lib/python2.7/site-packages/PyQt4/__init__.pyc'
>>> import PyQt4
>>> PyQt4
<module 'PyQt4' from '/usr/lib/python2.7/site-packages/PyQt4/__init__.pyc'>
>>> # __builtin__ is built-in
>>> import __builtin__
>>> __builtin__
<module '__builtin__' (built-in)>
In a program, you could use the __file__ attribute, but built-in modules do not have it.
>>> os.__file__
'/usr/lib/python2.7/os.pyc'
>>> PyQt4.__file__
'/usr/lib/python2.7/site-packages/PyQt4/__init__.pyc'
>>> __builtin__.__file__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__file__'