.
├── django_web
│ ├── RealTimeData
│ │ ├── init.py
│ │ ├── admin.py
│ │ ├── apps.py
│ │ ├── migrations
│ │ │ ├── 0001_initial.py
│ │ │ ├── init.py
│ │ ├── models.py
│ │ ├── sql_api_test.html
│ │ ├── templates
│ │ │ └── RealTimeData
│ │ │ └── index.html
│ │ ├── tests.py
│ │ ├── urls.py
│ │ └── views.py
│ ├── init.py
│ ├── db.sqlite3
│ ├── django_web
│ │ ├── init.py
│ │ ├── asgi.py
│ │ ├── settings.py
│ │ ├── urls.py
│ │ └── wsgi.py
│ └── manage.py
└── main.py
这是文件结构,同时 main.py 处在一个 django_web 的文件夹下。
现在的问题是:当我单独执行 main.py 文件时,导入 RealTimeData 的模块出现 ModuleNotFoundError 。
然而在配置 DJANGO_SETTINGS_MODULE 时引用 settings 文件是没问题的。sys.path 也包含了 main.py 的父目录的路径。
print(sys.path)
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_web.django_web.settings')
django.setup()
from django_web.RealTimeData.models import RowRecord
现在才发现提问题不给报错信息是多么愚蠢,程序在 django.setup()那一步就已经出错。
'''
(django_env) mac@Luxis-MacBook-Pro django_web % python -m main
Traceback (most recent call last):
File "/Users/mac/opt/miniconda3/envs/django_env/lib/python3.8/runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/Users/mac/opt/miniconda3/envs/django_env/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/Users/mac/Documents/gitee_dir/django_web/main.py", line 13, in <module>
django.setup()
File "/Users/mac/opt/miniconda3/envs/django_env/lib/python3.8/site-packages/django/init.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/mac/opt/miniconda3/envs/django_env/lib/python3.8/site-packages/django/apps/registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "/Users/mac/opt/miniconda3/envs/django_env/lib/python3.8/site-packages/django/apps/config.py", line 212, in create
mod = import_module(mod_path)
File "/Users/mac/opt/miniconda3/envs/django_env/lib/python3.8/importlib/init.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'RealTimeData'
'''
1
sudoy 2021-08-21 07:57:19 +08:00
加个空的 __init__.py 在 main.py 同级目录下面试试?
|
2
Trim21 2021-08-21 08:30:37 +08:00
|
3
wchluxi OP @Trim21 我在终端创建 django 项目,然后又在 pycharm 创建项目再倒入 django 项目的文件夹。结果是有三个 django_web 的同名文件夹。
main.py:”django_web/main.py” settings.py:”django_web/django_web/django_web/settings.py” 手机回复,排版可能不好,请见谅。 |
5
Trim21 2021-08-21 09:05:47 +08:00 via Android
python -m main 运行脚本报错会消失吗?
|
7
goodness1900 2021-08-21 11:25:46 +08:00 via iPhone
django_web.RealTimeData.models 文件的内容打出来看看,models.py 可能有 import 异常
|
8
goodness1900 2021-08-21 11:37:43 +08:00 via iPhone
另外,整个 django 项目的启动可能也会有问题。按照现在的项目路径(假设.是根目录,也就是./main.py ,./django_web 是同一级),那么如果按照 python -m main 的方式启动,./django_web/中所有模块在引入自身的模块时,如果采用绝对路径应该写成 django_web.xxx.xxx 。除非将./django_web 整个变成一个外部包进行 pip install,就可以像 requests 一样进行 import
|
9
wchluxi OP @goodness1900
'''python from django.db import models # Create your models here. class RowRecordManager(models.Manager): def create_rowrecord(self, date, acc_name, proj_name, unit_name, imp, clk, cost): rowrecord = self.create(r_date=date,r_account=acc_name, r_proj=proj_name,r_unit=unit_name, r_impression=imp,r_click=clk, r_cost=cost ) return rowrecord class RowRecord(models.Model): r_date = models.DateField() r_account = models.CharField(max_length=100) r_proj = models.CharField(max_length=100) r_unit = models.CharField(max_length=100) r_impression = models.DecimalField(max_digits=10, decimal_places=0) r_click = models.DecimalField(max_digits=10, decimal_places=0) r_cost = models.DecimalField(max_digits=10, decimal_places=2) objects = RowRecordManager() def __str__(self): return '%s,%s,%s,%s,%d,%d,%d' %(str(self.r_date), self.r_account, self.r_proj, self.r_unit, self.r_impression, self.r_click, self.r_cost) ''' |
10
wchluxi OP 现在才发现提问题不给报错信息是多么愚蠢,程序在 django.setup()那一步就已经出错。
(django_env) mac@Luxis-MacBook-Pro django_web % python -m main ['/Users/mac/Documents/gitee_dir/django_web', '/Users/mac/opt/miniconda3/envs/django_env/lib/python38.zip', '/Users/mac/opt/miniconda3/envs/django_env/lib/python3.8', '/Users/mac/opt/miniconda3/envs/django_env/lib/python3.8/lib-dynload', '/Users/mac/opt/miniconda3/envs/django_env/lib/python3.8/site-packages'] Traceback (most recent call last): File "/Users/mac/opt/miniconda3/envs/django_env/lib/python3.8/runpy.py", line 194, in _run_module_as_main return _run_code(code, main_globals, None, File "/Users/mac/opt/miniconda3/envs/django_env/lib/python3.8/runpy.py", line 87, in _run_code exec(code, run_globals) File "/Users/mac/Documents/gitee_dir/django_web/main.py", line 13, in <module> django.setup() File "/Users/mac/opt/miniconda3/envs/django_env/lib/python3.8/site-packages/django/__init__.py", line 24, in setup apps.populate(settings.INSTALLED_APPS) File "/Users/mac/opt/miniconda3/envs/django_env/lib/python3.8/site-packages/django/apps/registry.py", line 91, in populate app_config = AppConfig.create(entry) File "/Users/mac/opt/miniconda3/envs/django_env/lib/python3.8/site-packages/django/apps/config.py", line 212, in create mod = import_module(mod_path) File "/Users/mac/opt/miniconda3/envs/django_env/lib/python3.8/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1014, in _gcd_import File "<frozen importlib._bootstrap>", line 991, in _find_and_load File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "<frozen importlib._bootstrap>", line 1014, in _gcd_import File "<frozen importlib._bootstrap>", line 991, in _find_and_load File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked ModuleNotFoundError: No module named 'RealTimeData' |
11
goodness1900 2021-08-24 00:28:56 +08:00 via iPhone
|