About calling functions before modules written by yourself in python

PS: I was lazy before and used the method of adding system paths directly. Now the more the code is changed, the more complicated it is, and recently I have to add multiple configuration parameters. When I added the parameters and found that it collapsed, the service calculation has been failing, so I still have to use the guide The form of the package, I have used it a little before, but it has always been ambiguous. Now I know that once there are more code functions, I have to use it. I hope to understand it more intuitively.

 

1. Let me talk about the way to be lazy, which is the following code:

import sys, os
sys.path.append(os.path.dirname(__file__) + os.sep + 'database/')

If the running directory of the current py file is /home/pacs/PycharmProjects/tf, then the above code can be added to the current py file, and you can directly import it under /home/pacs/PycharmProjects/tf/database/ in the following code document. This is suitable for temporary use when there are few files.

 

2. The form of the guide package:

The following is the format of the package opened on pycharm

LungDetectorClassier is the top level of the package, and then each folder must be put in an __init__.py to know that this is a package to call the files in the folder (the LungDetectorClassier folder also needs to be added, which is the last __init__.py) .

If I want to call configg.py (cross-folder) in the db_sqlite3.py file, then write like this

from LungDetectorClassier.config import configg

And I want to call db_sqlite3.py (under the same folder) in execute_status_result.py, then write like this

from . import db_sqlite3

 

important point

If we want to start the service, we cannot put the running file of the calling service in the folder inside the package, but must be placed outside the package as follows:

I use test_ai.py and test_predict.py to start the service.

The code of test_ai.py is as follows:

# -*- coding: utf-8 -*-
from LungDetectorClassier.service.ai import AI_algorithm

if __name__ == '__main__':
    AI_algorithm()

And test_predict.py is similar.

Guess you like

Origin blog.csdn.net/qq_36401512/article/details/95204458