Python Forum

Full Version: Import from .py trouble
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to split my code into separate modules: sprites.py, setting.py, etc. I'm clearly missing something.

from settings import *
print(tile_size)
This doesn't work, though I think it should.
Error:
Traceback (most recent call last): File "C:\Users\owner\Desktop\Python3\DungeonGame\levels.py", line 2, in <module> print(tile_size) NameError: name 'tile_size' is not defined
If I spell the file wrong, like "sttings", I get
Error:
Traceback (most recent call last): File "C:\Users\owner\Desktop\Python3\DungeonGame\levels.py", line 1, in <module> from sttings import * File "C:\Users\owner\AppData\Local\Programs\Thonny\lib\site-packages\thonny\backend.py", line 305, in _custom_import module = self._original_import(*args, **kw) ModuleNotFoundError: No module named 'sttings'
I have all the python files in the same directory and setting.py's first line is "tile_size = 50" Python can see that the file exists but "import" doesn't make it visible.
Could there be another settings.py file that it could be getting confused with? Try renaming settings something weird like 'blargoraftical,' and try 'import blargoraftical'.
Files are now in own folder and I renamed everything.

from blargoraftical import * 
print(tile_size)
and
import blargoraftical  
print(tile_size)
Error:
Traceback (most recent call last): File "C:/Users/owner/Desktop/Python3/imporation/test.py", line 2, in <module> print(tile_size) NameError: name 'tile_size' is not defined
Same result in 2 editors. I have other programs that run fine. I'll try a restart.
It is possible the directory where settings.py is located to be different from the running file. I can reproduce a similar error if I do so.
Got it. Human error. Confused about two files named "settings". I'll catch that next time, Thank-you.