I am trying to run from the directory
folder:
$ python subdirectoryTwo/file.py
command (Python 2.7).
Folders structure:
-directory
-subdirectoryOne
__init.py__
config.py
-subdirectoryTwo
__init.py__
file.py
My file.py
has: from subdirectoryOne.config import config
However I am getting an error:
file.pyImportError: No module named subdirectoryOne.config`
(I guess it still looks in the directory
folder)
There few thins you need to change.
(public)landpacks-MacBook-Pro:qx frank$ tree
.
├── __init__.py
├── __init__.pyc
├── a
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── config.py
│ └── config.pyc
└── b
├── __init__.py
└── test.py
Create a __init__.py
with with your subdirectoryOne
and subdirectoryTwo
like here I used a
and b
. and then add few codes at the begin of your file.py
. And I name it as test.py
here. the code is:
import sys
sys.path.append("..")
from project.a.config import myconf
print(myconf)
You can see I import it by project.a.config
instead of a.config
. because you run your code under the project.
UPDATE
My a/config.py
just simple with:
(public)landpacks-MacBook-Pro:qx frank$ cat a/config.py
myconf='127.0.0.1'
解决方案之一(不是最佳解决方案)是将PYTHONPATH
设置为您的directory
:
$ export PYTHONPATH='/absolute/path/to/directory'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.