python导入导致名称错误 [英] python import results in nameerror

查看:62
本文介绍了python导入导致名称错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这看起来很基本,所以我一定遗漏了一些明显的东西.目标是从同一目录导入模块.我已经尽可能简单地将其分解,然后得到 nameerror.

This seems pretty basic, so I must be missing something obvious. Goal is to import a module from the same directory. I've broken it down about as simple as I can and I'm getting the nameerror.

文件 import_this.py:

def my_function(number) :
    print number + 2

文件 import_test.py:

import import_this
my_function(2)

我是否必须指定导入文件所在的目录?(它与测试文件相同).另外,我可以测试看看导入了哪些模块吗?

Do I have to specify the directory the import file is in? (It's in the same as the test file). Also, can I test to see what modules are imported?

推荐答案

您访问的函数不正确.

要么使用以下内容

import import_this
import_this.my_function(2)

或者做,

from import_this import my_function
my_function(2) 

这篇关于python导入导致名称错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆