python中找不到模块_Python在同一文件夹中找不到模块

1586010002-jmsa.png

My python somehow can't find any modules in the same directory.

What am I doing wrong? (python2.7)

So I have one directory '2014_07_13_test', with two files in it:

test.py

hello.py

where hello.py:

# !/usr/local/bin/python

# -*- coding: utf-8 -*-

def hello1():

print 'HelloWorld!'

and test.py:

# !/usr/local/bin/python

# -*- coding: utf-8 -*-

from hello import hello1

hello1()

Still python gives me

>>> Traceback (most recent call last):

File "", line 1, in

File "", line 4, in

ImportError: No module named hello

What's wrong?

解决方案

Your code is fine, I suspect your problem is how you are launching it.

You need to launch python from your '2014_07_13_test' directory.

Open up a command prompt and 'cd' into your '2014_07_13_test' directory.

For instance:

$ cd /path/to/2014_07_13_test

$ python test.py

If you cannot 'cd' into the directory like this you can add it to sys.path

In test.py:

import sys, os

sys.path.append('/path/to/2014_07_13_test')

Or set/edit the PYTHONPATH

And all should be well...

...well there is a slight mistake with your 'shebang' lines (the first line in both your files), there shouldn't be a space between the '#' and the '!'

There is a better shebang you should use.

Also you don't need the shebang line on every file... only the ones you intend to run from your shell as executable files.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值