gpt4 book ai didi

Ubuntu中的Python导入模块和文件

转载 作者:行者123 更新时间:2023-12-04 18:44:06 25 4
gpt4 key购买 nike

我需要从位于 myprojectdir/gaapp/models.py 的 models.py 导入类到另一个位于 myprojectdir/dataUpdater/updateApi.py 的 python 文件, 在 Linux Ubuntu

我的代码:

from .models import FinalPage, FinalUser, Activity, Sessions, UserD, PageView, Event

我得到的错误:
Traceback (most recent call last):
File "./updateApi.py", line 23, in <module>
from .models import FinalPage, FinalUser, Activity, Sessions, UserD, PageView, Event
ModuleNotFoundError: No module named '__main__.models'; '__main__' is not a package


有什么建议么?

编辑:
(myprojectenv) llewellyn@ubuntu-s-1vcpu-2gb-ams3-01:~/myprojectdir/gaapp$ ls -la
total 84
drwxrwxr-x 8 llewellyn llewellyn 4096 May 13 15:04 .
drwxrwxr-x 7 llewellyn llewellyn 4096 May 13 13:06 ..
drwxrwxr-x 3 llewellyn llewellyn 4096 May 7 08:52 .idea
-rw-rw-r-- 1 llewellyn llewellyn 0 May 7 07:21 __init__.py
drwxrwxr-x 2 llewellyn llewellyn 4096 May 13 14:30 __pycache__
-rwxrwxr-x 1 llewellyn llewellyn 86 May 12 17:39 admin.py
-rwxrwxr-x 1 llewellyn llewellyn 194 May 13 13:25 apps.py
drwxrwxr-x 3 llewellyn llewellyn 4096 May 7 09:04 config
drwxrwxr-x 2 llewellyn llewellyn 4096 May 13 06:22 ga-credentials
drwxrwxr-x 3 llewellyn llewellyn 4096 May 13 07:15 migrations
-rw-rw-r-- 1 llewellyn llewellyn 2529 May 13 07:07 models.py
-rwxrwxr-x 1 llewellyn llewellyn 12390 May 13 07:06 myprojectdir
drwxrwxr-x 3 llewellyn llewellyn 4096 May 7 09:28 templates
-rwxrwxr-x 1 llewellyn llewellyn 83 May 12 17:41 tests.py
-rw-rw-r-- 1 llewellyn llewellyn 0 May 12 17:40 update.out
-rwxrwxr-x 1 llewellyn llewellyn 265 May 13 11:01 urls.py
-rwxrwxr-x 1 llewellyn llewellyn 16313 May 13 14:30 views.py

最佳答案

import sys
sys.path.insert(0, '/home/llewellyn/myprojectdir/gaapp/')
import models

这对我有用。即使现在我遇到了 django 错误
Traceback (most recent call last):
File "./updateApi.py", line 27, in <module>
import models
File "/home/llewellyn/myprojectdir/gaapp/models.py", line 7, in <module>
class UserD(models.Model):
File "/home/llewellyn/myprojectdir/myprojectenv/lib/python3.6/site-packages/django/db/models/base.py", line 107, in __new__
app_config = apps.get_containing_app_config(module)
File "/home/llewellyn/myprojectdir/myprojectenv/lib/python3.6/site-packages/django/apps/registry.py", line 252, in get_containing_app_config
self.check_apps_ready()
File "/home/llewellyn/myprojectdir/myprojectenv/lib/python3.6/site-packages/django/apps/registry.py", line 134, in check_apps_ready
settings.INSTALLED_APPS
File "/home/llewellyn/myprojectdir/myprojectenv/lib/python3.6/site-packages/django/conf/__init__.py", line 76, in __getattr__
self._setup(name)
File "/home/llewellyn/myprojectdir/myprojectenv/lib/python3.6/site-packages/django/conf/__init__.py", line 61, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.


任何意见,将不胜感激

关于Ubuntu中的Python导入模块和文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61780163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com