How to put .py code files in a folder?

Question:
Hello!

I’m trying to put all of my .py code files in a folder instead of the root directory. See image below for the desired state of affairs.

Is there a way to do this without having to specify a hardcoded path name in each import statement? I’m hoping I can just modify the replit file or something similar. The main.py file can be at the root level or in the folder. Thanks!

python_code_in_folder

Repl link:
NONE

NONE
2 Likes

I’m not exactly sure what you mean. If you want to put the main.py file inside of the code folder, you can modify .replit to have the run command be run = 'python code/main.py'.

If you want to import the files inside of the folder into your main.py you will have to add an __init__.py file into the code folder. This file should be empty, but tells Python that it is a module. You can then do the following.

# main.py
from code import aggro, armory, char,spell

aggro.foo()
char.func()
# Execute your functions here inside of your code

Hope that helps!

I just went into the .replit file and where it say entry put the name of the of the folder the main.py is in like in the picture below and it worked for me but I do not know if that is what you meant hope it helps