Python modules commonly used - package & cross-module code calls

Python modules commonly used - package & cross-module code calls

First, the package (Package)

As more and more of your module file, you need to divide the module file, such as the interaction with the database are responsible for putting a folder to put the relevant page of interacting with a folder.

my_proj/
├── apeland_web  #代码目录
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── manage.py
└── my_proj    #配置文件目录
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

Like above, a plurality of folder management module files, the folder is called packet

A package is a folder, but the folder must exist under the init .py file, the contents of the file can be empty, the init .py the current folder is used to identify a package.

This the init .py file is mainly used to package some initialization, and when this package is currently being called by another program, the init .py file will be executed first, usually empty, some of you want to be called as long as the package is executed immediately the code can be placed in the init .py years.

Second, cross-module import

Directory structure is as follows

my_proj
├── apeland_web
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── manage.py
└── my_proj
    ├── settings.py
    ├── urls.py
    └── wsgi.py

According to the above structure, how to achieve apeland import my web / views.py inproj/settings.py module?

Directly into the case, it will complain that module not found

Because the path is not found, my_proj / settings.py equivalent? Apeland_web / views.py father (apeland_web) brother (my_proj) son (settings.py), settings.py regarded cousin views.py friends, in views.py can only import the same level in the code module brothers, or sublevels bag module, we do not know exist cousin cousin. what should we do?

The answer is to add environment variables, adding the path to sys.path father class, you can, so to import the equivalent grade from father to start looking for a module.

apeland_web / views.py add environment variables

import sys ,os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) #__file__的是打印当前被执行的模块.py文件相对路径,注意是相对路径
print(BASE_DIR) # 输出是/Users/alex/PycharmProjects/apeland_py_learn/day4_常用模块/my_proj 
sys.path.append(BASE_DIR)
from  my_proj import settings
print(settings.DATABASES)

Third, the official recommendation of the Cross-import method

Although you can import cross-module by adding an environment variable way, but officials do not recommend doing so, because it will need to write again to add the code environment variables in each program under each directory.

The official recommended play is to create a program entry in the project, the whole process should be initiated by calling the start of the program from the entrance, the entrance procedure is generally placed in the top-level directory of the project.

The advantage of this is that the project level two apeland_web / views.py in recalling his cousin my_proj / will not need to add environment variables when settings.py.

The reason is because manage.py on the top floor, the environment variable path manage.py start of the project will automatically become ... .xxx / my_proj / at this level

Guess you like

Origin www.cnblogs.com/Kwan-C/p/11620962.html