如何解决“NameError: name 'null' is not defined"?尝试在 Python 2.7 中导入任何模块时出错 [英] How can I resolve a "NameError: name 'null' is not defined" error while trying to import any module in Python 2.7

查看:86
本文介绍了如何解决“NameError: name 'null' is not defined"?尝试在 Python 2.7 中导入任何模块时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天安装了python并成功使用pip install获取了一些模块.但我无法导入任何 .py 模块.

I installed python today and used pip install successfully to get some modules. But I am unable to import any .py modules.

我在与我尝试运行的代码相同的目录中创建了 mod.py(它有一个简单的打印命令).我也卸载并重新安装了anaconda.但错误仍然存​​在.任何人对如何解决这个问题有任何想法?谢谢!

I created mod.py (it has a simple print command) in the same directory as the code I am trying to run. I uninstalled and reinstalled anaconda as well. But the error persists. Anyone with any ideas on how to fix this? Thanks!

import mod

NameErrorTraceback (most recent call last)
<ipython-input-1-18de99490651> in <module>()
----> 1 import mod

C:\Users\Mayank\mod.py in <module>()
      3   {
      4    "cell_type": "code",
----> 5    "execution_count": null,
      6    "metadata": {},
      7    "outputs": [],

NameError: name 'null' is not defined

这是 .py 代码在编辑器中的样子的示例(同样的问题):

This is a sample of what the .py code looks like in the editor (same problem):

{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def fib(n):    # write Fibonacci series up to n\n",
    "    a, b = 0, 1\n",
    "    while b < n:\n",
    "        print b,\n",
    "        a, b = b, a+b\n",
    "\n",
    "def fib2(n):   # return Fibonacci series up to n\n",
    "    result = []\n",
    "    a, b = 0, 1\n",
    "    while b < n:\n",
    "        result.append(b)\n",
    "        a, b = b, a+b\n",
    "    return result"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}

推荐答案

null 不像在 JavaScript 中那样是保留字.使用 None.

null isn't a reserved word like it is in JavaScript. None is used.

这篇关于如何解决“NameError: name 'null' is not defined"?尝试在 Python 2.7 中导入任何模块时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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