【问题标题】:Python in Ubuntu importing modules and filesUbuntu中的Python导入模块和文件
【发布时间】:2020-08-29 23:20:39
【问题描述】:

我需要将位于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

【问题讨论】:

标签: python linux ubuntu import git-bash


【解决方案1】:
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.

任何建议将不胜感激

【讨论】:

    【解决方案2】:

    您必须正确设置 PYTHONPATH。我猜您应该将其设置为指向您的myprojectdir 目录。然后在你的代码中你会说

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

    请注意,您需要在要从中导入模块的每个目录或包中都有一个__init__.py

    您遇到的具体错误是因为您不能在主模块中使用相对导入(以. 开头)。在这里对此进行了长时间的讨论:https://www.python.org/dev/peps/pep-0366/ 但结果是,直到/除非实现这一点,相对导入仅在包内有效。

    【讨论】:

    • Traceback (most recent call last): File "./updateApi.py", line 23, in &lt;module&gt; from gaapp.models import FinalPage, FinalUser, Activity, Sessions, UserD, PageView, Event ModuleNotFoundError: No module named 'gaapp'
    • 您的 PYTHONPATH 设置为什么?您是否在所有包中都创建了__init__.py
    • 我正在尝试导入 models.py(我正在使用 django).. 所以 gaapp 中有一个 init.py 文件.. 你是这个意思吗?生病现在发送内容.. gaapp 是 django 应用程序
    • 再一次,除非您将 PYTHONPATH 设置为指向正确的位置,否则它将不起作用。
    • 另外,我在 Linux 服务器上编码(使用数字海洋)所以我不确定 PATH。使用服务器时如何将 PATH 设置为指向正确的位置
    猜你喜欢
    • 1970-01-01
    • 2016-08-04
    • 2019-08-04
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    相关资源
    最近更新 更多