Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Import from .py trouble
#1
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.
Reply
#2
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'.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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.
Reply
#4
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.
Reply
#5
Got it. Human error. Confused about two files named "settings". I'll catch that next time, Thank-you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Having trouble with a circula import SuchUmami 2 723 Jul-05-2023, 06:28 PM
Last Post: deanhystad
  Trouble with "from…import…as" arbiel 2 2,748 Apr-14-2020, 03:38 PM
Last Post: arbiel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020