I’m trying to create a basic blogging application in Python using Web.Py. I have started without a direcotry structure, but soon I needed one. So I created this structure:
Blog/
├── Application/
│ ├── App.py
│ └── __init__.py
|
├── Engine/
│ ├── Connection/
│ │ ├── __init__.py
│ │ └── MySQLConnection.py
│ ├── Errors.py
│ └── __init__.py
├── __init__.py
├── Models/
│ ├── BlogPostModel.py
│ └── __init__.py
├── start.py
└── Views/
├── Home.py
└── __init__.py
start.py imports Application.App, which contains Web.Py stuff and imports Blog.Models.BlogPostModel, which imports Blog.Engine.Connection.MySQLConnection.
Application.App also imports Engine.Errors and Views.Home. All these imports happen inside contructors, and all code inside all files are in classes. When I run python start.py, which contains these three lines of code:
from Application import App
app = App.AppInstance()
app.run()
The following stack trace is printed:
Blog $ python start.py
Traceback (most recent call last):
File "start.py", line 2, in <module>
Blog = App.AppInstance()
File "/home/goktug/code/Blog/Application/App.py", line 4, in __init__
from Blog.Views import Home
ImportError: No module named Blog.Views
But according to what I understand from some research, this should run, at least until it reaches something after App.py. Can anyone tell where I made the mistake? (I can provide more code on request, but for now I’m stopping here, as this one is getting messier and messier).
App.pycontains the statementSo
Blogneeds to be among the list of directories Python searches for modules (sys.path). That can be arranged in various ways.Since you are starting the app with
python start.py, the directorycontaining
start.pyis automatically added to the search path. Soyou could change
to
Another option would be to move
start.pyup one level, out of theBlogdirectory. Then when you callpython start.py, thedirectory containing
start.pywill also be the directorycontaining
Blog. So Python would findBlogwhen executingfromBlog.Views ...
Finally, you could add the
Blogdirectory to your PYTHONPATH environmentvariable.