Reputation: 79
I am not able to import a module under the simplest of implementations. I have looked at other SO posts and they are much more complicated and hard for me to follow. I imagine this is a Python configuration error but I would like to be sure before I go digging up any libraries or path statements if it is not necessary.
[modone.py]:
import modtwo.py
mod1 = "I am module #1"
print(mod1)
print(mod2)
[modtwo.py]:
mod2 = "I am module #2"
Doesn't get more basic than that... yet this is the output that I receive in the shell:
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 "D:\OTI\Scripts\Python\Script1\modone.py", line 1, in <module>
import modtwo.py
ImportError: No module named 'modtwo.py'; modtwo is not a package
Upvotes: 1
Views: 7411
Reputation:
Use the following:
import modtwo # no .py extension
print(modtwo.mod2) # mod2 is namespaced to its module.
Alternatively, you can do
from modtwo import mod2
print(mod2)
While the second option may be more tempting (shorter when you use mod2
often, it doesn't namespace mod2
and thus could clash with an identically named variable in your modone
module.
Upvotes: 1