Python Forum
Import from .py trouble - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Import from .py trouble (/thread-22814.html)



Import from .py trouble - michael1789 - Nov-27-2019

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.


RE: Import from .py trouble - ichabod801 - Nov-27-2019

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'.


RE: Import from .py trouble - michael1789 - Nov-27-2019

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.


RE: Import from .py trouble - Ray - Nov-27-2019

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.


RE: Import from .py trouble - michael1789 - Nov-27-2019

Got it. Human error. Confused about two files named "settings". I'll catch that next time, Thank-you.