无法通过 python 路径访问 python 文件
Unable to access python file via python path
我有一个具有以下架构的项目:
source/
├── data
│ ├── job.py
│ ├── __init__.py
│ └── __pycache__
│ └── article_library.cpython-35.pyc
├── graph
├── __init__.py
├── main.py
└── settings
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-35.pyc
│ └── settings.cpython-35.pyc
└── settings.py
这是我的 python 路径:
['', '/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu', '/usr/lib/python3.5/lib-dynload', '/home/user/Documents/thesis/venv/lib/python3.5/site-packages']
source/settings/settings.py
MY_TOKEN = "atokentodostuff"
source/__init__.py
from settings import settings
source/main.py
from settings import settings
print(settings.MY_TOKEN)
它输出给我:
atokentodostuff
source/data/job.py
from ..settings import settings
print(settings.MY_TOKEN)
但是当我解释这段代码时(无论我在项目中的位置如何)我得到了以下错误:
Traceback (most recent call last):
File "job.py", line 3, in <module>
from ..settings import settings
SystemError: Parent module '' not loaded, cannot perform relative import
如何将 settings.py
中的变量访问到文件 job.py
中。有没有办法让它可以从任何地方启动?
尝试以下 source/data/job.py
的代码片段
from source.settings import settings
print(settings.MY_TOKEN)
我有一个具有以下架构的项目:
source/
├── data
│ ├── job.py
│ ├── __init__.py
│ └── __pycache__
│ └── article_library.cpython-35.pyc
├── graph
├── __init__.py
├── main.py
└── settings
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-35.pyc
│ └── settings.cpython-35.pyc
└── settings.py
这是我的 python 路径:
['', '/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu', '/usr/lib/python3.5/lib-dynload', '/home/user/Documents/thesis/venv/lib/python3.5/site-packages']
source/settings/settings.py
MY_TOKEN = "atokentodostuff"
source/__init__.py
from settings import settings
source/main.py
from settings import settings
print(settings.MY_TOKEN)
它输出给我:
atokentodostuff
source/data/job.py
from ..settings import settings
print(settings.MY_TOKEN)
但是当我解释这段代码时(无论我在项目中的位置如何)我得到了以下错误:
Traceback (most recent call last):
File "job.py", line 3, in <module>
from ..settings import settings
SystemError: Parent module '' not loaded, cannot perform relative import
如何将 settings.py
中的变量访问到文件 job.py
中。有没有办法让它可以从任何地方启动?
尝试以下 source/data/job.py
的代码片段from source.settings import settings
print(settings.MY_TOKEN)