python导入模块路径错误_Python导入模块错误

I have a problem in import of modules in my project. I was creating tests and I cant import my main to test one end point of my application, from the test file teste_ex.py. This is my project structure:

backend_api/

api/

__init__.py

main.py

testes/

__init__.py

test_ex.py

In my test_ex.py Im trying to import main in this way:

import api.main

from webtest import TestApp

def test_functional_concursos_api():

app = TestApp(main.app)

assert app.get('/hello').status == '200 OK'

I omly get ImportError: No module named 'api'

What can be done to import my main to my test file?

解决方案

Here's my best guess as to what might help:

You should make sure that the path to the directory in which your main.py file resides can be found in sys.path. You can check this yourself by running this snippet in test_ex.py:

import sys

for line in sys.path:

print line

If the directory in which main.py is found is not included in your path, you can append that path to sys.path, like this (include this snippet in test_ex.py):

import sys

sys.path.append("/path/to/your/module/backendapi/api/")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值