当前位置:   article > 正文

python如何调用另一个py文件的函数,从另一个文件调用Python函数

python如何调用另一个py文件的函数csdn

This problem has confused me for days.

I have two files, helpers.py and launcher.py.

In helpers.py I have defined the function hello(), which prints "hello".

I want to call hello() in launcher.py.

This is what I wrote in launcher.py:

from helpers import hello

....

helpers.hello()

But when I run it, I get this:

from helpers import hello

ImportError: No module named helpers

How do I fix this?

Edit in response to answers / comments

I'm using OS X and Python 3.4

The two files are in the same directory

I tried the two ways:

from helpers import hello

hello()

and

import helpers

helpers.hello()

But still this bug:

import helpers

ImportError: No module named 'helpers'

I think there should be something wrong in the CLASSPATH of Terminal.

Second edit

The problem highlighted in these answers was an issue, but in the end resetting the classpath resolved.

解决方案

I reset the CLASSPATH and it works fine somehow. Weird problem. Thanks everyone!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/423502
推荐阅读
相关标签
  

闽ICP备14008679号