Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
module run on import?
#1
I have this game I am working on. I am trying to use a config.py file for global variables like this.
from os import system

clear_screen = system("cls")
quit_game = exit(0)
output:
Output:
PS C:\Users\mcmxl>
I have also tried this. and get an attribute error.
from os import system

if __name__ == "__main__":
    clear_screen = system("cls")
    quit_game = exit(0)
Error:
Guess a letter. Traceback (most recent call last): File "c:/Users/mcmxl/mystuff/hangman/hangman.py", line 46, in main guess_letter = input("Guess a letter. ") KeyboardInterrupt During handling of the above exception, another exception occurred: Traceback (most recent call last): File "c:/Users/mcmxl/mystuff/hangman/hangman.py", line 112, in <module> main() File "c:/Users/mcmxl/mystuff/hangman/hangman.py", line 49, in main config.clear_screen AttributeError: module 'config' has no attribute 'clear_screen'
I don't know what else to do.
Reply
#2
Define functions in config.py
from os import system
import sys

def clear_screen():
    system("cls")

def quit_game():
    sys.exit(0)
Then in your program, call the functions (notice the parentheses)
...
config.clear_screen()
Reply
#3
@mcmxl22, and what is the purpose to have these two in a config file? First of all its not configuration file per se (i.e. there is no configuration in what you do). Second, it just obscure what is done, why not simply use system('cls') instead of whatever implementation you go for? Third - cls is Windows. For example on Linux the command is clear. Using cls makes your game platform-specific.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
@buran My reasoning for the config file was to try to simplify the game file. I thought it was getting too long and complicated.

Thanks for pointing out the cross-platform issue. I'm thinking I'll use a try/except to fix it and put it in its own function.

@Gribouillis thanks for the explanation of putting it in functions.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  is import cointegration_analysis a recognized module mitcht33 1 385 Nov-06-2023, 09:29 PM
Last Post: deanhystad
  problem in import module from other folder akbarza 5 1,260 Sep-01-2023, 07:48 AM
Last Post: Gribouillis
  can not import anaconda pandas module. PySpark pandas module is imported!! aupres 0 680 Aug-06-2023, 01:09 AM
Last Post: aupres
  import module error tantony 5 3,361 Dec-15-2022, 01:55 PM
Last Post: Lauraburmrs
  Import a module one step back of the path prathampatel9 1 1,036 Sep-21-2022, 01:34 PM
Last Post: snippsat
  Import a module for use in type hint? Milosz 0 1,455 Nov-08-2021, 06:49 PM
Last Post: Milosz
  Can't install nor import delorean module Tek 3 2,741 Oct-27-2021, 03:32 AM
Last Post: Tek
  import module with syntax error Skaperen 7 5,161 Jun-22-2021, 10:38 AM
Last Post: Skaperen
  'urllib3' Module not found when import 'requests' spanz 5 9,960 Jan-06-2021, 05:57 PM
Last Post: snippsat
  Problem with Flask Bcrypt import module marcello86 2 5,597 Aug-31-2020, 08:10 PM
Last Post: marcello86

Forum Jump:

User Panel Messages

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