Python imports from subfolders

2020-06-12 03:34发布

I am attempting to grade some python submissions that are in separate folders for each student. To do this, there is a function, say f() which I want to run. I understand that if my current path is the same as the one where the file is located, I can simply do

import filename
filename.f()

However, are there better ways? For instance, let's say the directory structure is as follows:

main.py
student/run_this.py

I know that if there is a "__init__.py" file in the student folder, I can just type

import student.run_this

However, without that file, it doesn't work.

Some similar questions I found were

but none of these gave particularly satisfying answers.

1条回答
走好不送
2楼-- · 2020-06-12 04:06

create an __init__.py module inside the folder student which should contain

from . import *

You can then call any modules from student folder to its parent folder modules as

import student.module.py

If you post any other errors you are facing, we can help further.

查看更多
登录 后发表回答