为什么不能从文件导入函数?

2024-10-04 03:21:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个文件gufu.py,它大约有4个函数:

def parse1():
    ...
def parse2():
    ...
def parse3():
    ...
def parse4():
    ...

但是当我通过pythonshell运行以下命令时:

^{pr2}$
  • 不过,我是从同一个目录运行python

  • 我通过:
    import os; os.chdir("/path/to/thefile"); os.getcwd()也是。


更新

>>> import os; os.chdir("/thepath/to/the/file")
>>> os.getcwd()
'/thepath/to/the/file'
>>> from gufu import *
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'gufu'
>>> os.path.isfile('gufu.py')
False
>>> os.getcwd()
'/thepath/to/the/file'
>>> os.listdir()
['tests', 'urls.py', '__init__.py', 'models.py', 'tests.py', 'views.py', 'database.py', 'models.py.bak', 'hashers.py', 'apps.py', 'injection__backup.py', '__pycache__', 'urldata.py', 'Injection.py', 'gufu.py', 'Xss.py', 'templates', 'helpers', 'migrations', 'forms.py', 'models.bakFULLTBL', 'static', 'admin.py']

问:有人能帮我理解为什么这不起作用吗?在


Tags: thetopathpyimportosmodelsdef