How to import a Custom Class?

I would like to do some OOP. I have the code working if I just declare the class in the main.py file but for obvious reasons that is getting a bit over crowded. Ive created a new newClass.py file in the same mode directory. In the main.py script, I try both

import "newClass" and from "newClass" import *

In both cases I’m getting the ImportError: No module named newClass

I’m guessing it could be a permission issue or I’m just dumb and don’t know what I’m doing. Both are equally likely.

Here is the output from console

error reloading: Traceback (most recent call last):
  File "/home/music/EYESY_OS/engines/python/etc_system.py", line 258, in reload_mode
    imp.load_source(self.mode, self.mode_root+'/main.py')
  File "/sdcard/Modes/Python/ZHS - Boids//main.py", line 6, in 
    from Boids import *
ImportError: No module named Boids

@chrisk anything you could say on this :grimacing:

Here is one way to do it. Say you have the file test.py in the folder with main.py:

class TestClass:
    def hi(self):
        print 'hi'

Then in main.py you can do this:

import imp

def setup(screen, etc) :
    test = imp.load_source('test', etc.mode_root +  'test.py')
    obj = test.TestClass()
    obj.hi()

def draw(screen, etc) :
    pass

You can import a specific class or the whole package. You place import statements at the top of your source files

Thanks ill give that a try and report back

yea that’s what I was doing so not sure why its not working. I wonder if its related to another issue im having. Certain things arent working as expected. I developed the mode in VScode and items like using the pygame.math.vector2 class are not accessible once I move over to the eyesy even though I have the import pygame.math at the top of the script.

Also basic sys commands like type() are not running on the eyesy but do in the emulator on VS code.

The following code produces the subsequent print statement in the Eyesy console for example:

try:
        print("----------result type: " + str(type(vectorToLimit)))

image\

whereas in the VScode environment it outputs as expected

image

Let me know if you have any insights as to the related issue i shared below.