问题
I have been looking for a way to solve this.
I have a python project, and this is the folder structure I want:
/project/main.py
/project/src/models.py
/project/test/tests.py
I want to be able to run the tests by executing the tests.py in terminal. tests.py imports modules in /project/src/ for testing. First I solved this by adding
sys.path.insert(0, '..')
in tests.py. But then the paths used in models.py for opening text files had to be relative to the tests.py, etc. Which means the program wouldn't run when excecuted from main.py, cause of the paths.
I also tried with dots when importing modules into tests.py, like from ..src.models import *
, but that gave error message saying "Attempted relative import in non-package".
What should I put in the top of tests.py to be able to import the modules from models.py?
回答1:
The structure you're using is not one I would recommend, but I'm a comparaitive newb to how Python projects are usually structured. I believe this will do what you're after:
1) Place an __init__.py
file inside /project
, /project/src
, and /project/test
to make sure they're treated as packages.
2) Place from __future__ import absolute_import
at the top of each Python file.
3) Then use relative imports:
test.py
:
from ..src import models
main.py
:
from .src import models
4) You'll need to start your application differently. Ensure your current directory is the parent of /project
(which appears to be the file system root) and run your project this way:
python -m project.main
For my own project, I would definitely put main.py
inside src
if it's the start point of your application. I might put tests.py
in src
, too, but if not, I would add /project/src
to the test runner's Python path on the command line instead of in code.
I would still use absolute_import
regardless. In my experience, it's a very clean solution to module organization, and it's also how things work by default in Python 3.
回答2:
first put your main.py in the src directory..
in your tests you can do , sys.path.append('the src directory')
if you like to force the execution to be in specific directory regardless from where you are executing the app i suggest you adding
import os
os.chdir('relative path to the src dir')
thisway your program will run in the directory you specified so it will respect the relative paths you have in your code
来源:https://stackoverflow.com/questions/20693687/python-import-modules-folder-structures