I'm very new to python and I have a directory structure like this:
root
--child
-----config.py
example.py
In example.py
I just tried:
import child
but that doesn't seems to be working.
Where I'm making the mistake!
If you want to import config.py
with importing child
you need to define child
as a package.
To do so you need to create an __init__.py
file in your child
directory.
Check this about packages
Do you have a __init__.py
file in root/child/
directory? After creating this file you should be able to do this:
import child.config
or
from child import config
You can also import multiple modules from child directory like this:
from child import first, second, third
Read about modules and packages here .
Your directory should have init .py so that python will understand it is a package. So the directory structure would be like
root
__init__.py
child
__init__.py
config.py
example.py
import root.child
note, you should import root.child
not child
.
Create an empty __init__.py
file in same directory with config.py
. This is required for importing files like a package.
Then you can import it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.