Trouble remembering import rules in Python

73 views Asked by At

It's been a while since I have used Python and am stumbling already at a simple import!

statements.py:

str = "hello"

main.py:

import statements

print statements.str

Obviously the final program will have more going on, and the statements will be stuff like URLs. For this simple example however with both files sitting side by side in the same folder I get the following error on running main.py:

AttributeError: 'module' object has no attribute 'str'

I know I am doing something terribly silly, but I can't see what it is. Searching around it looks fine. Any help/insight is appreciated.

1

There are 1 answers

0
Martijn Pieters On BEST ANSWER

You used the import statement correctly. The error then can be caused by either importing a different module with the same name, or by having altered the module after already having imported it.

You can check print statements.__file__ to see what module Python found instead when importing, and you can use the reload() function to ask Python to reload the module from disk if you altered it.