Ask coding questions

← Back to all posts
Run files in a folder?
Avirah (0)

Hi,

If I have a file named 'test.py' in a FOLDER called 'folder', how can I execute that file from 'main.py'?

I understand if the file is NOT in the folder, I can use:
'import test'. However 'test.py' is in a folder, and so I cannot use the import command.

Does anyone know a solution to this?

Thanks in advance!

Answered by ruiwenge2 (1105) [earned 5 cycles]
View Answer
Comments
hotnewtop
ruiwenge2 (1105)

do something like this:

import folder.file

with an init.py file, make sure the name is __init__.py. then you can import it with just the folder name:

import folder
ShluffyMonster (4)

I am not sure, but would /folder/<filename>.py or folder/<filename>.py work? I am not good with python though, still learning that

Avirah (0)

@codingjlu Unfortunately that does not seem to work. I've even tried adding in the 'init.py' file as suggested further down in that forum. I feel like it runs slightly differently on replit... Thoughts?

codingjlu (480)

@Avirah I think some more googling should easily solve your problem... that's what I usually do and it works 99% of the time GOOOOO GOOGLE lol no I'm not affiliated with it