0

I have two modules:

Main:

variable1, variable2 = 800, 800

Other:

import main
variable1, variable2 = main.variable1, main.variable2

I tried to use import main to access the mains variable, however this will reinitialize the entire main class. That is contrary to what I want. I only want a single value from the variable 1 and variable two into this other file without initialization. What can I do?

When I comment out:

#from main import DISPLAY_W, DISPLAY_H

It will run the map for example.RPG Map

When I uncomment, I get this:

Traceback (most recent call last):
  File "D:/Downloads/Programming/Python/Programs/Learning Basics/Program 7.12 - 2D pygame, Snow/main.py", line 7, in <module>
    import Weather
  File "D:\Downloads\Programming\Python\Programs\Learning Basics\Program 7.12 - 2D pygame, Snow\Weather.py", line 2, in <module>
    from main import DISPLAY_W, DISPLAY_H
  File "D:\Downloads\Programming\Python\Programs\Learning Basics\Program 7.12 - 2D pygame, Snow\main.py", line 143, in <module>
    Weather.initialize_weather()
AttributeError: module 'Weather' has no attribute 'initialize_weather'

This implies I provided no attribute, however note this, I used dir(Weather), to find my problem:

['DISPLAY_H', 'DISPLAY_W', 'MAX_RAIN', 'MAX_SNOW', 'RAIN_SIZE', 'Rain', 'Random', 'SNOW_SIZE', 'SPEED', 'Snow', 'SystemRandom', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'betavariate', 'choice', 'choices', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'initialize_rain', 'initialize_snow', 'initialize_weather', 'lognormvariate', 'normalvariate', 'paretovariate', 'pygame', 'rainfall', 'randint', 'random', 'randrange', 'sample', 'screen', 'seed', 'setstate', 'shuffle', 'snowfall', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate']

Then again with the main uncommented:

['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'pygame']

Its erasing my values?

In weather

import pygame
from main import DISPLAY_W, DISPLAY_H
from random import *

DISPLAY_W, DISPLAY_H = 800, 800
screen = pygame.display.set_mode((DISPLAY_W, DISPLAY_H))

In main:

import pygame
import glob
from random import *
from pygame import mixer
import TileSheet
import Button
import Weather

# Load Window
pygame.init()
# 800x800 pixels
DISPLAY_W, DISPLAY_H = 800, 800
canvas = pygame.Surface((DISPLAY_W, DISPLAY_H))
screen = pygame.display.set_mode((DISPLAY_W, DISPLAY_H))
pygame.display.set_caption('Test Tileset')
running = True
  • 1
    What do you mean by reinitialize ? And they are just `modules`, not `classes`. – han solo Jun 06 '21 at 16:06
  • importing main RERUNS the entire main module, so i cant for example use import main or from main import variable1. I have to use a specified number for BOTH classes, but I already defined them into the main class, so I want to duplicate this value. So when i go back and change the main value, they will BOTH change. –  Jun 06 '21 at 16:19
  • I am not entirely sure if i got it, but the `modules` are only run to completion only once. Unless you forcefully `reload` it. And how are you going to change the values in `main.py` ? – han solo Jun 06 '21 at 16:23
  • I will show you. –  Jun 06 '21 at 16:27
  • Could you please paste the code ? You seem to be importing `Weather` in `main.py` and `main` in `Weather.py`, which is kinda creating a cycle. Maybe you want to reconsider that first ? – han solo Jun 06 '21 at 16:43
  • You might have a point about that, but the main class MUST have the weather class, the weather class only needs two values for the screen height and width. –  Jun 06 '21 at 17:00
  • Maybe create a third file(could be a config file too) and `import/load` the display height and width from that in both the `main.py` and `Weather.py` ? – han solo Jun 06 '21 at 17:13
  • I don't see how thats sufficient for resources. –  Jun 06 '21 at 17:15
  • Well, for one, that could solve the cyclic import issue, you seem to have. But i don't understand what do you mean by sufficient for resources. – han solo Jun 06 '21 at 17:17
  • I haven't seen one 2D rpg game that did that. Neither professionally. –  Jun 06 '21 at 17:21

1 Answers1

0

You can add if __name__ == "__main__": in your main.py file.

For example:

if __name__ == "__main__":
    print('Hello')

In the example above program will print Hello only if you run main.py from that module directly and it won't print anything when you import main.py in other modules.

Read this for more details.