Skip to content
← Back to Community
How to run additional python files in main.py? (discord bot)
Profile icon
Fourzee

I'm trying to clean up my python code so that different parts are in separate files but I'm unsure on how to get these files to run in my main python file. I've done:

from file_name import *

but it says it is unused, and that it is unable to detect undefined names. When i try to run the code in discord, it works fine with any code in main.py but does nothing when i try code from my separate files.

Voters
Profile icon
Fourzee
Comments
hotnewtop
Profile icon
Squirrel777

Ok, first add a python file, were going to be using example.py.

Second, add this code to example.py

print("Hi!")

And finally, your going to add this code to main.py:

import example #You can do "import <fileName>", so you can apply this to any file!
Profile icon
CodingCactus

import filename will run the file. You can then do filename.somethingElse to get a function or variable from the file

from filename import somethingElse will import a specific function or variable from that file. You can then just do somethingElse to use the thing you imported

from filename import * imports all the functions, variables etc from the file and all of them can be access with just their name rather than filename.somethingElse

I think in your case you may just want to do import single_response

Profile icon
FREEFIREFIRE31

@CodingCactus what do you mean by somethingElse please give an example

Profile icon
RYANTADIPARTHI

one way is to go to the shell, and type in python FILENAME.py, so replace FILENAME with the file you want to run.