程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How to add module packages to the system directory by Django front and back end classification

編輯:Python

1. stay pycharm Medium manage Console creation function app

Django establish app The order is

startapp user


As shown in the figure , Create features app (user) success .

2. Create application folder apps, Put all the functions app Put it in its directory and mark

         establish python software package (apps), Be careful Don't create directories Otherwise, the problem of package guide may occur in the later development process .python The package in is equivalent to the module , It can be found directly .

establish apps After the folder succeeds , Will function app Throw it in , After that, the developers are creating all the app In the process, you need to drop the file into apps Module package .

Throw in apps File does not mean the end of module file configuration , At this point, you must apps Directory in pycharm Marked as The root directory of the resource , If you don't mark the project, you can't find the function created by the developer app.

 

3. stay setting Register function in app, take app Add path to system path .

At this time in setting File to register the function created app(user).

If there is no warning, the creation is successful .

After the above basic operations are realized , At this point, the last thing developers need to do is to change the function app Add the path of to the middle of the system path ,

What is the system path ?

It's ours python route , That is to say, the module app and python Of sys Package to achieve unity

There are two ways to write this

The first one is (Django2 Version writing ), Use it directly os Module utilization sys Of insert Insert it :

That is

import sys
import os
sys.path.insert(0, BASE_DIR)
sys.path.insert(1, os.path.join(BASE_DIR, 'apps'))

The second is Django3 Writing , No longer use os Spliced , Instead, use path splicing ( Simple and crude ):

import sys
import os
sys.path.insert(0, str(BASE_DIR))
sys.path.insert(1, str(BASE_DIR / 'apps'))

Two contrasts :

So the above is Django The solution to how to add the module package to the system directory during project development .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved