1. python生產環境, 多層modules 導入問題:
多個modules 如何導入不同級別的包:
在每個modules下新建 __init__.py
import os, sys
dir_mytest = os.path.dirname(os.path.abspath(__file__)) # crontab 跑時 os.getcwd() 只會取到用戶的路徑....
sys.path.insert(0, dir_mytest+"tmp\\t1")
#sys.path.insert(0,os.getcwd())
在控制目錄的執行文件中:
from module import py
如果是三層結構或者更多:
from module.submodule.submodule.py import *
2. django 結構:
->1. 在admin層 urls.py 設置路由:
# 導入app模塊的控制層
from app_name import views
urlpatterns = [
#path('admin/', admin.site.urls),
path('index/', views.index)
]
->2. 在app業務處理層 views.py 編寫處理邏輯:
#導入HttpResponse模塊
from django.shortcuts import HttpResponse
def index(request):
return HttpResponse("hello world")
在此可以進行python的業務編碼處理。
->3. 在app業務處理層, settings.py 設置數據庫連接:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app_name'
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', #此項不變
'NAME': 'mydata', #設置的數據庫名稱
'USER': 'root', #mysql 的登錄用戶名
'PASSWORD': '123456', #mysql的登錄密碼
'HOST': 'localhost', #默認為127.0.0.1
'PORT': '3306', #默認端口:3306
'charset': 'utf8',
}
}
import pymysql
pymysql.install_as_MySQLdb()
conda install mysqlclient=1.3.13
mysqlclient 1.3.3 or newer is required; you have 0.7.11.None
通過查找路徑C:\Programs\Python\Python36-32\Lib\site-packages\Django-2.0-py3.6.egg\django\db\backends\mysql\base.py
這個路徑里的文件把
if version < (1, 3, 3):
raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % Database.__version__)
query = query.decode(errors='replace')
將這段代碼注釋掉
########### pyecharts ##############################################################
安裝pyecharts: pip install pyecharts
script_list 是 Page() 類渲染網頁所需要依賴的 echarts js 庫,依賴的庫的數量取決於所要渲染的圖形種類。
echarts js 庫的地址,默認的地址為 http://chfw.github.io/jupyter-echarts/echarts
在app_name下, 創建templates目錄;
-> 在該目錄下:
:創建一個static:存放echarts.js
: 創建一個pyecharts.html:
<head>
<meta charset="utf-8">
<title>Proudly presented by PycCharts</title>
{% for jsfile_name in script_list %}
{% load staticfiles %}
<!-- <script src="{% static '/echarts/{{jsfile_name}}.js' %}"></script> -->
<script src="/static/echarts/{{jsfile_name}}.js"></script>
{% endfor %}
</head>
<body>
<!--|safe 過濾器, 不對字符串進行轉義-->
{{myechart|safe}}
</body>
######################## debug ###############################
報錯:
UnicodeDecodeError: 'gbk' codec can't decode byte 0xa6 in position 9737:
打開:
File "E:\software\Anaconda\lib\site-packages\django\views\debug.py", line 332, in get_traceback_html
t = DEBUG_ENGINE.from_string(fh.read())
在331行修改: .open(encoding="utf-8") as fh