python中找不到模块_Python-找不到模块

1586010002-jmsa.png

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值