I'm a Python newbie, so bear with me :)
I created a file called test.py with the contents as follows:
test.py import sys print sys.platform print 2 ** 100
I then ran import test.py
file in the interpreter to follow an example in my book. When I do this, I get the output with the import error on the end.
win32 1267650600228229401496703205376 Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named py
Why do I get this error and how do I fix it? Thanks!
To get rid of this error “ImportError: No module named”, you just need to create __init__.py in the appropriate directory and everything will work fine.
Python's ImportError ( ModuleNotFoundError ) indicates that you tried to import a module that Python doesn't find. It can usually be eliminated by adding a file named __init__.py to the directory and then adding this directory to $PYTHONPATH .
The __init__.py file makes Python treat directories containing it as modules. Furthermore, this is the first file to be loaded in a module, so you can use it to execute code that you want to run each time a module is loaded, or specify the submodules to be exported.
The Python "ImportError: cannot import name" occurs when we have circular imports (importing members between the same files). To solve the error, move the objects to a third file and import them from a central location in other files, or nest one of the imports in a function.
Instead of:
import test.py
simply write:
import test
This assumes test.py is in the same directory as the file that imports it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With