如何从另一个目录调用Python脚本

时间:2018-07-07 16:22:13

标签: python django ubuntu

Django project folder structure

    project (source root)
    -->apps
    ---->**app1**
    ----->admin.py
    ----->init.py
    ----->apps.py
    ----->models.py
    ----->urls.py
    ----->views.py
    ----->callscript2.py
    ----->**myscipt-folder**
    ------>script1.py
    ------>script2.py
    ------>__init__.py
    ---->**app2**
    ----->admin.py
    ----->init.py
    ----->apps.py
    ----->models.py
    ----->urls.py
    ----->views.py
    ----->sample.py (must be able to call the script2 from app1, myscript folder)
    ---->**app3**

当我从sample.py

调用文件时
from apps.app1.myscipt-folder import script1

我收到以下错误

from apps.app1.myscipt-folder import script1
ModuleNotFoundError: No module named 'apps.app1'; 'apps' is not a package

但是当我在callscript2.py内对其进行测试时,它可以正常工作。您认为我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

代替from apps.app1.myscipt-folder import script1
试试这个:

from app1.myscipt-folder import script1