简体   繁体   中英

how to import own modules in Python?

I have a main.py file and I need to import the database tables that are in "entity / models.py". How do I do ? I use linux and I don't want to add models.py to sys.path, I want models.py to be visible only in this project. I also don't want to create symbolic links because in my opinion it's the same thing as putting models.py inside the app itself.

在此处输入图片说明

You can convert the directory into a package with a __init__.py file. Then as usual you can import the module from the package as in:

from entity.models import func

The __init__.py file can just be an empty file. Just its existence will treat the directory as a package.

Hope this answer helps.

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.

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