简体   繁体   English

Python包/模块导入

[英]Python package/module import

I've gone through a lot of resources, but I simply cannot get this to work. 我已经花了很多资源,但是我根本无法使它正常工作。 I am trying to do an absolute import for some config files and library class files of my own, but the import fails. 我正在尝试为自己的某些配置文件和库类文件进行绝对导入,但是导入失败。 Here is a simple example: 这是一个简单的示例:

└── import_test
    ├── __init__.py
    ├── config
    │   ├── __init__.py
    │   └── database.py
    └── services
        ├── __init__.py
        └── data.py

Here is the database.py 这是database.py

mysql = {'host':    'localhost',
         'user':    'root',
         'passwd':  'password',
         'db':      'db_name',
         'charset': 'utf8'}

and here is data.py 这是data.py

from import_test.config import database

When I run the data.py, I get this error. 当我运行data.py时,出现此错误。

ModuleNotFoundError: No module named 'import_test'

What am I doing wrong? 我究竟做错了什么?

Thanks! 谢谢!

将您的顶级工作目录添加到PYTHONPATH环境变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM