Skip to content
Sign upLog in
← Back to Community
Noob here: How do I call a python script from another python script?
Profile icon
[deleted]

So i don't want my main.py to be cluttered, and I can't seem to get my python script to import a python script from the same folder. How does it work on the online IDE?

Comments
hotnewtop
Profile icon
Nayoar

No

@gbafana25
, it does work - you just need to create another file my_module.py (or whatever you want to call it) and do import my_module wherever you want to use it.

If you have my_function in my_module.py which is inside the folder my_folder, you can do

import my_folder.my_module my_folder.my_module.my_function()

or you can do

from my_folder.my_module import my_function my_function()

Hope you found this helpful!
Upvote for vee freebucks

Profile icon
Woloszin

@minx28

how do i import .py files from a folder to my main.py? I could use os.chdir('/home/runner/AuxPrograms') import and get back, but I know that is a aesy way to do it...

Profile icon
Nayoar

@Woloszin
if you have module.py in folder.py you can do import folder.module i think

Profile icon
Woloszin

@minx28
that works well! tks,
@minx28

Profile icon
Nayoar

@Woloszin
np, how about an upvote? ;)

Profile icon
Woloszin

@minx28
new here... how do i do it? do you have a specific post?

Profile icon
Nayoar

@Woloszin
you click the thing next to my name that is a number with an arrow on top, but it looks like you already have, thx

Profile icon
gbafana25

You can call other scripts in the same directory with just a regular import statement. You don’t need the file extension.
Example import my_module

Edit: Oh sorry, I didn’t see the part about the online IDE, nvm.

Profile icon
[deleted]

@gbafana25
I just tried the import statement. it works well! Thank you!