How to import python script files?

So i created the Custom fps counter and now i would like to know how to import it to my other .py file(s)?

Just as with any other importation: if it’s in the same directory as the file that you want to import it into, you should be able to just do this:

from <name of your FPS-counter file, without .py> import FPScounter

Don’t forget to remove the code in the FPS-counter’s file that runs it as a stand-alone program!

However, note that as presented in the thread to which you linked, you’ve made your FPS-counter a sub-class of DirectObject. I’m not sure of whether it’s valid to have more than one DirectObject in a single program. However, I’ll let others weigh in on that, as I’m not confident in the matter myself.

So what do i need to remove from the code exactly? the DirectObject and fpscounter = FPScounter()
run()?

That seems correct to me, yes. :slight_smile:

so i put it as from fpscounter import fpscounter and got ImportError: cannot import name ‘fpscounter’ from

In the thread that you linked to, you have capital letters in your class-name, but you’re attempting to import it without. Matching capitalisation matters in this, I do believe! (The same goes for the name of the file from which you’re importing the class.)

hmm yes the class has capital letters but the file name is fpscamera.py and it doesn’t work, do i need to rename the class name then?

No–as long as the capitalisation of both the bit before “import” and the bit after it match their respective elements (the file-name and the class-name, respectively), it should be fine, I think.

hm interesting, its imported now and nothing shows up on the screen

Well, you’ve imported it, but have you instantiated it? That is, have you created an instance of the class in your program?

If you have, then perhaps the OnscreenImage in the class is being covered by something else?

Hello

Python does not work like other languages concerning imports/includes.

Python is always interpreted. If you import a file that you think only defines e.g. a sum, python will execute that sum when you import the file.

This is ok as long as you really only define stuff like classes or functions with the ‘class’ and ‘def’ keywords but it leads to unexpected behaviour when important stuff happens at top level. In your case the counter creation and run().

Avoid this by creating a function that does these things and putting it into a if __name__=="__main__": condition. This is what tells python to really only run that code when the file is called directly, but not when it is imported.

In your case you will also want to import the class definition by using “from myfile import myclass” and then by executing the constructor myclass() somewhere before run().

I suspect that at the moment you import, which creates the thing and runs, and then your other program is defined and run. And there is of course no overlap because you have two runs and the counter does not show up in the second run.

Does that make sense?

Yours is a general python question though. You can ask for how to import correctly on general python forums or channels, if you would like a different explanation.

yeah it makes sense, right now my fpscounter code looks like this:

`import direct.directbase.DirectStart
import time
from direct.gui.OnscreenText import OnscreenText
from direct.task import Task
from panda3d.core import TextNode

red = (1,0,0,1)
yellow = (1,0.9,0,1)
green = (0,0.9,0,1)

class fpscounter():
def init(self,):
self.frameTime = 0
print(“Fps Counter starting…”)
base.setBackgroundColor(1, 1, 1)
self.fpsTextobject = OnscreenText(text=" framerate: " + str(round(self.frameTime,2))+ " fps", pos=(0.80, 0.90), scale=0.07,bg = (0.3,0.3,0.4,1)) # placeholder
taskMgr.add(self.fpscounterTask, ‘fps1’)
self.fpsTextobject.setFg(green)

    self.maxfps = 144


def fpscounterTask(self,task):
    self.frameTime = globalClock.getAverageFrameRate()
    #self.frameTime = 60

    color = green
    if self.frameTime <= 144:
        color = green
    if self.frameTime <= 60:
        color = yellow
    if self.frameTime <= 30:
        color = red

    self.fpsTextobject.destroy()
    self.fpsTextobject = OnscreenText(text=" framerate:   " + str(round(self.frameTime,2)) + " fps", pos=(0.55, 0.90),scale=0.07, fg=(color))
    self.fpsTextobject.setAlign (TextNode.ALeft)
    #self.fpsTextobject.setFg(color)

    return task.cont

`

Your code looks more or less fine in terms of being able to run, I think. (Although I’m not sure about importing DirectStart.) So as I asked above Max’s post, are you instantiating your FPS-counter class in the program that you’re importing it into?