尝试在 Python 2.7 中导入任何模块时如何解决“NameError: name 'null' is not defined”错误

May*_*ria 10 python python-2.7

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

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

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
Run Code Online (Sandbox Code Playgroud)

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

{
 "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
}
Run Code Online (Sandbox Code Playgroud)

小智 14

null不像在 JavaScript 中那样是保留字。None用来。


May*_*ria 5

我想通了这个问题。我正在为我正在编写的代码做“另存为”mod.py。相反,我需要“另存为“mod”,然后“下载为”.py 文件类型。由于在 Jupyter 中保存/创建模块文件的方式,这只是一个问题。昨天对我的问题的回答有助于我弄清楚这一点。谢谢大家!