Programmatically importing module via importlib - __path__ not set? - python

I'm trying to import a sub-module programmatically. My file tree looks like this:
oopsd/__init__.py
oopsd/oopsd.py
oopsd/driver/__init__.py
oopsd/driver/optiups.py
The optiups.py simply prints "Hello World".
The oopsd.py looks like this:
import importlib
importlib.import_module('oopsd.driver.optiups')
Now with this, I'm getting this exception:
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 1521, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "oopsd/oopsd.py", line 29, in <module>
sys.exit(main())
File "oopsd/oopsd.py", line 23, in main
loaddriver()
File "oopsd/oopsd.py", line 26, in loaddriver
importlib.import_module('oopsd.driver.optiups')
File "/usr/lib/python3.3/importlib/__init__.py", line 90, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1586, in _gcd_import
File "<frozen importlib._bootstrap>", line 1567, in _find_and_load
File "<frozen importlib._bootstrap>", line 1514, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 313, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1586, in _gcd_import
File "<frozen importlib._bootstrap>", line 1567, in _find_and_load
File "<frozen importlib._bootstrap>", line 1524, in _find_and_load_unlocked
ImportError: No module named 'oopsd.driver'; oopsd is not a package
Does __path__ even still exist in Python 3?
I also tried importing .driver.optiups instead, but this yields:
TypeError: relative imports require the 'package' argument
__package__ seems unset, so I'm lost.
How do I do this the right way?

This is an old question, but since it was bumped, the other answer is totally wrong, and this is a common problem:
You're probably doing this.
python oopsd/oopsd.py
Don't do this. :)
Specifically, NEVER try to directly run a file that's part of a parent package. When you run python FILENAME, Python adds the file's containing directory to sys.path, and DOESN'T add the current directory. So you have oopsd/ in your path, and every module in oopsd/ just became a top-level module. Python has no way of even knowing that any of them are supposed to have an oopsd. prefix, because the parent directory doesn't exist anywhere in sys.path.
If you want to execute a module directly, do this:
python -m oopsd.oopsd
This puts the current directory in sys.path and ensures that imports of your source tree work as you'd expect them to.
Alex Z's answer is wrong because it doesn't actually fix this problem, and it's not a relative import — implicit relative imports no longer exist in Python 3.

This works for me (relative path to driver):
import importlib
importlib.import_module('driver.optiups')
(karthikr in comments was close, but you don't seem to need the leading .)

Related

ImportError when debugging but not when running in PyCharm

I am building a desktop app using the python library Kivy. Since I installed a plugin to be able to visualize .kv files, I am not able to debug my code. Yet I am able to run it without problems. This is the error I keep getting:
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "...\Worktime_Tool\venv\lib\site-packages\kivy\app.py", line 416, in <module>
from os.path import dirname, join, exists, sep, expanduser, isfile
ImportError: cannot import name 'sep' from 'ntpath' (...\AppData\Local\Programs\Python\Python310\lib\ntpath.py)
So far I do not understand where this ntpath stands for. I tried to compare the ntpath while debugging and while running and they are the same. I also double checked that the interpreter set for the prrject and the interpreter in the run/debug configurations are the same.
I tried with and without the usage of a venv .
Does anyone have an idea why I can run this app but not debug it?
Appreciate much the help!
I had the same problem a while ago. You can either edit the \lib\site-packages\kivy\app.py in your virtual environment to remove the sep from that import line on line 416.
It will look like this after you remove the sep.
from os.path import dirname, join, exists, expanduser, isfile
Or
Update to the latest version of kivy which no longer gives me the error.
You can update to the latest kivy developer build with this command:
python -m pip install "kivy[base] # https://github.com/kivy/kivy/archive/master.zip"

IDAPython with PyCharm throws: "ModuleNotFoundError: No module named '_ida_hexrays'"

I want to use PyCharm with IDAPython. I setup the Python Interpreter by adding the following path to the "Interpreter Paths":
C:\Program Files\IDA Pro 7.7\python\3
After that, it recognize the modules idaapi and idautils which is a good sign.
I wrote a simple scripts to test it:
import idaapi
import idautils
filename = idaapi.get_input_file_path()
I set a breakpoint on the filename... row and it failed with:
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:\Program Files\IDA Pro 7.7\python\3\ida_hexrays.py", line 12, in <module>
import _ida_hexrays
ModuleNotFoundError: No module named '_ida_hexrays'
It doesn't recognize the modules _ida_hexrays. How can I make it work?
I see that there is a file: C:\Program Files\IDA Pro 7.7\python\3\ida_64\_ida_hexrays.pyd.
In the ida_hexrays.py it throws the error when it try to import it like that:
# Import the low-level C/C++ module
if __package__ or "." in __name__:
from . import _ida_hexrays
else:
import _ida_hexrays

Sphinx throws KeyError exception

I have a python flask script inside a docker. This script get some environment variable from the docker-compose in this way:
app = Flask(__name__)
DEFAULT_PSW = os.environ['DEFAULT_PSW']
#app.route("/")
def manager():
...
I wanted to document it using Sphinx. I was able to configure it but when I run "make html" I get this exception:
WARNING: autodoc: failed to import module 'main'; the following exception was raised:
Traceback (most recent call last):
File "/home/lorenzo/.local/lib/python3.7/site-packages/sphinx/ext/autodoc/importer.py", line 32, in import_module
return importlib.import_module(modname)
File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/lorenzo/Desktop/Dev/project/script/manager_flask/manager/manager/main.py", line 20, in <module>
DEFAULT_PSW = os.environ['DEFAULT_PSW']
File "/usr/lib/python3.7/os.py", line 679, in __getitem__
raise KeyError(key) from None
KeyError: 'DEFAULT_PSW'
If I've understood well how sphinx works the problem is that sphinx will try to "compile" my script and, of course, during this process it won't find the environment variable. What can I do?
EDIT:
Steps that I did to get this error:
Create folder called docs
Inside the docs folder started sphinx-quickstart leaving everything by default except for Author and Project name.
Edited the conf.py (this file can be found in the repository in the comment)
Generate the rst file with sphinx-apidoc
Create folder modules and added the file scp_handler.rst and main.rst
Updated the index.rst adding the path to the new generated file
Ran "make html".
For documentation purposes set and export the key in the conf.py file:
os.environ("DEFAULT_PSW", "default_psw")

"KeyError: 'PYTHONPATH'". Sites.py broken in python3?

I think this got foobarred when I was doing some brew update stuff. Everytime my system python3 is called, it emits:
Error in sitecustomize; set PYTHONVERBOSE for traceback:
KeyError: 'PYTHONPATH'
This is particularly annoying because I use powerline in my terminal and powerline makes several calls to python each time I execute a command.
Calling Python3 with a the Verbose flag I get:
$ PYTHONVERBOSE=True python3
<multiple pages of python startup info...>
# possible namespace for /usr/local/lib/python2.7/site-packages/backports
# bytecode is stale for 'sitecustomize'
# code object from /usr/local/lib/python2.7/site-packages/sitecustomize.py
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 481, in execsitecustomize
import sitecustomize
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "/usr/local/lib/python2.7/site-packages/sitecustomize.py", line 15, in <module>
str(sys.version_info[0]) + '.x!\n PYTHONPATH is currently: "' + str(os.environ['PYTHONPATH']) + '"\n' +
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/os.py", line 669, in __getitem__
raise KeyError(key) from None
KeyError: 'PYTHONPATH'
# destroy sitecustomize
import 'site' # <_frozen_importlib_external.SourceFileLoader object at 0x101e3a630>
<more python startup info...>
So the system Python3 is trying to load the Python2 version of sitecustomize? I suspect that's the problem. If so, what are my options for fixing this? brew unlink python3 && brew link python3 didn't help.
I'm not sure what the bytecode is stale for 'sitecustomize' comment python is making is about, but I've also removed the sitecustomize.pyc file, which didn't help.

Logging module not working with Python3

I am having issues with the standard logging module. If I open a python2.7 shell and import logging everything works fine:
$ python
>>> import logging
>>>
But if I open a python3.4 shell and import logging I get the following error:
$ python3.4
>>> import logging
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2222, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 2164, in _find_spec
File "<frozen importlib._bootstrap>", line 1940, in find_spec
File "<frozen importlib._bootstrap>", line 1916, in _get_spec
File "<frozen importlib._bootstrap>", line 1897, in _legacy_get_spec
File "<frozen importlib._bootstrap>", line 863, in spec_from_loader
File "<frozen importlib._bootstrap>", line 904, in spec_from_file_location
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/logging-0.4.9.6-py3.4.egg/logging/__init__.py", line 618
raise NotImplementedError, 'emit must be implemented '\
^
SyntaxError: invalid syntax
I have no idea what the problem is and can't seem to find anyone else who has had the same issue.
logging module is by default there in Python 3 environment .No need to import it.
You seem to have installed a third party library called logging in your Python 3 environment, which is hiding the standard library version, and one of its files has a typo.
I stupidly created a file called logging.py to try out some log features. Then when trying the code below, it is effectively referring to itself and can't find the debug method.
import logging
logging.debug("Debug message")
Changing my file name to logtest.py fixed the problem.
for me it was the previous install of a logging library under an older Python version.
pip3 uninstall logging
fixed it for me.

Categories

Resources