How can I resolve a "NameError: name 'null' is not defined" error while trying to import any module in Python 2.7

55,885

Solution 1

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

Solution 2

I figured out the issue. I was doing 'Save As' mod.py for the code I was writing. Instead, I needed to 'Save As 'mod' and then 'Download As' .py file type. This was only an issue due to the way the module file is saved/created in Jupyter. The responses to my question yesterday were helpful in me to figure this out. Thanks, everyone!

Solution 3

null is not pre-defined in Python. You may have gotten it by copying and pasting a JSON object. Use None in place of null to denote null objects.

Solution 4

I think the problem was that your original saved file was not in python format. I was having the same problem as when I saved from Jupytr. The file had an extension .ipynb which is a python notebook format.

To convert the .ipynb to a .py (which is a text file) I ran the following command in the terminal window:

 $ jupyter nbconvert --to script mynotebook.ipynb

This created a file "mynotebook.py" which I could then run using

$ python mynotebook.py

in the terminal window.

Solution 5

I had this issue just remaining the ipynb file to .py. What worked for me is to just copy the script from the ipynb file to a new empty .py file.

Share:
55,885
Author by

Mayank Seksaria

Updated on December 03, 2021

Comments

  • Mayank Seksaria about 1 year

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

    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
    

    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
    }
    
  • Mayank Seksaria almost 4 years
    Hi - thanks for the response. As a beginner I am not sure I follow. I am not sure what the source attribute isn’t or how I would remove it. Is it possible for you to expand on your you suggestion please? Thanks again.
  • gmds
    gmds almost 4 years
    Open mod.py, ctrl-F "source" and copy everything after the :" to the next ".
  • Mayank Seksaria almost 4 years
    Hi - thanks for the response (this is a really great community) but I have no idea where the null is coming from actually. The code for mod.py is basically just a simple print command. What is a JSON object? Thanks again.
  • Mayank Seksaria almost 4 years
    Nathan - thanks. But I dont follow (beginner here). Could you please expand on your idea?
  • Nathan Hamm almost 4 years
    Yeah. Somewhere in your code you're referencing null which you haven't defined and isn't defined by Python, so you get a name error aka this variable doesn't exist error
  • Mayank Seksaria almost 4 years
    For reference, mod.py is very basic: import time def foo(): print time.time()
  • Mayank Seksaria almost 4 years
    I am trying this on my home computer and its the same problem. I have another random .py file fibo.py and I posted the code for it above. I was confused about what I am supposed to move after "source" and to where. Apologies for the continuing questions!
  • Mayank Seksaria almost 4 years
    I tried importing on spyder and it seems to be working. So this appears tied to Jupyter somehow? What am I doing wrong I wonder?
  • zelfde
    zelfde over 1 year
    Thanks a lot for this Answer. It helped make my day not suck
  • cphlewis
    cphlewis about 1 year
    This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review
  • matak8s about 1 year
    How to do that in colab?
  • matak8s about 1 year
    How to do that conversion in colab?