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 definedThis 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
} 1 11 Answers
null isn't a reserved word like it is in JavaScript. None is used.
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!
0null 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.
That is code from a Jupyter notebook, which is stored in JSON format and cannot be run directly by Python. To fix it, remove everything that doesn't correspond to the 'source' attribute.
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.ipynbThis created a file "mynotebook.py" which I could then run using
$ python mynotebook.pyin the terminal window.
2I 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.
2I fixed this by running every cell at least one time, so "execution_count": is changed to 1. Then save .py file.
I had the same error when I received a request data and used 'print(json.dumps(data, indent=1))'. I was able to fix it by using 'pprint(data)' instead.
None is used to indicate the absence of data, None is considered a value, and while None indicates the absence of data, None is different from a value of type str, int or float.
The type None is, in many cases, "null" or "empty" can be used as a value, but there is no official "null" type in Python.
Better try this : go to the directory where the module file should be present and create a text file with name mod.py
/users/abc/python/mod.py
now open this file using jupyter doc explorer :
and now add your code. for example :
mod.py (file content as below) :
def sum1(x,y): z=x+y return z
Just define the null on the top of your code like
null = ""and after this, the null will be replaced by the empty string ("") in your data. Thanks.
4