I am a beginner with Python. Before I start, here's my Python folder structure
-project
----src
------model
--------order.py
------hello-world.py
Under src I have a folder named model which has a Python file called order.py which contents follow:
class SellOrder(object):
def __init__(self,genericName,brandName):
self.genericName = genericName
self.brandName = brandName
Next my hello-world.py is inside the src folder, one level above order.py:
import model.order.SellOrder
order = SellOrder("Test","Test")
print order.brandName
Whenever I run python hello-world.py it results in the error
Traceback (most recent call last):
File "hello-world.py", line 1, in
import model.order.SellOrder
ImportError: No module named model.order.SellOrder
Is there anything I missed?
解决方案
All modules in Python have to have a certain directory structure. You can find details here.
Create an empty file called __init__.py under the model directory, such that your directory structure would look something like that:
.
└── project
└── src
├── hello-world.py
└── model
├── __init__.py
└── order.py
Also in your hello-world.py file change the import statement to the following:
from model.order import SellOrder
That should fix it :)
P.S.: If you are placing your model directory in some other location (not in the same directory branch), you will have to modify the python path using sys.path.