Hi I have a Jupyter Notebook, with python 2.7.13 and it worked fine all along. But starting today it shows strange behaviour.
when I do this in a fresh notebook Python 2:
print 'hello'
it returns this:
File "<ipython-input-1-bfbe230352b8>", line 1
print 'hello'
^
SyntaxError: invalid syntax
It was working ok till yesterday and today I restarted the computer and this happens.
Any reasons? How to fix?
I have checked anaconda, spyder, command prompt... everywhere else is ok with 2.7.13
print ('hello') works but I don't want this.. all my code is in Python 2.
I have now tracked the problem to be caused by the extension. Once I rename this file C:\Users\X\.jupyter\nbconfig\notebook.json to something else and reopen jupyter notebook, the print works as expected.
However, I do need the extensions at least the key ones I use regularly. I don't know which one to disable. I didn't know how to track the culprit without going through each one by one. Also, as far as I can remember, all the extensions I have been using for few weeks already without any issues.
These are the contents of the notebook.json file.
{
"load_extensions": {
"toc2/main": true,
"Runtools": true,
"collapsible_headings/main": true,
"codefolding/main": true,
"varInspector/main": true,
"runtools/main": true,
"runtools": true
},
"collapsible_headings": {
"add_button": true,
"add_insert_header_buttons": true
}
}
After trial and error with disabling one every time, restart and check.. I have now solved the problem by setting this line to false, it now works. The problem is with variable inspector extension.
"varInspector/main": false,
Sometimes the jupyter required a restart and sometime it didn't. Just opening the .ipynb file was enough to test.
I have the same extension running in my office computer, which has no issues.
If someone could really put in the comments for the cause and a solution for permanent fix of this problem will be great.
Could you please try with reloading sys like below ?
import sys
reload(sys)
print 'hello'
Related
I was watching Corey Schafer's video on YouTube on 'Setting up a Python Developer Environment in sublime Text'.
I installed the anaconda package, but for some reason, the gutter marks (for linting) are not showing in the line number column.
I have not altered the default settings.
I have added Corey's user settings to the sublime-anaconda user settings file:
{
"auto_formatting": true,
"autoformat_ignore":
[
],
"pep8_ignore":
[
"E501"
],
"anaconda_linter_underlines": false,
"anaconda_gutter_marks": true,
"anaconda_linter_mark_style": "none",
"display_signatures": false,
"disable_anaconda_completion": true,
"python_interpreter": "/usr/local/bin/python3"
}
I should note that I have also tried adding:
"anaconda_gutter_marks": true,
to no avail.
I have also saved the settings and restarted sublime text.
You need to change your "python_interpreter" setting to the actual path of the python.exe executable on your system. Opening cmd and entering which python may be helpful in finding it. Once you've found it, change and \ characters to / - for example, C:\Python38\python.exe would become C:/Python38/python.exe.
Where you've copied the code from correy's GitHub there is a line called "python_interpreter": "/usr/local/bin/python3" just delete that line and restart sublime and the errors should be showing again.
Check the screenshot:
My current work project is on writing a Python program that must at various points rely on R. Since I don't know R so well, and the person helping me doesn't know Python so well, the actual R code is not in my program. Instead, he opened Notepad, put the R code in there, and saved it as (name).r. When executed, the output is written into a txt file, which Python can then read.
All I have to do is ask Python to ask R to run (name).r
I've tried using subprocess.run. That worked for awhile, and then for some unknown reason stopped working and now does nothing. Then I tried using rpy2, which also worked for awhile; but now it looks like the installation is broken and I'm having trouble getting it reinstalled.
I'd like to give a 3rd option a try now: PypeR. I used pip install pyper. Looked like it was successful.
To keep things simple, I opened Notepad and typed in the following, and saved it as hello.r:
message <- 'goodbye'
write.table(message,'C:/Users/(my name)/Desktop/(folder)/goodbye.txt',row.names=FALSE,col.names=FALSE)
Manually opening R and copy-pasting the lines in one-at-a-time does indeed work. But I'm having trouble getting it to work from Python. Here are some things I've tried (I always put import pyper at the top):
pyper.runR("source('C:/Users/(muy name)/Desktop/(folder)/hello.r')")
This gives NameError: name 'dump_stdout' is not defined
pyper.R("source('C:/Users/(my name)/Desktop/(folder)/hello.r')")
This gives FileNotFoundError: [WinError 2] The system cannot find the file specified
r=pyper.R("C:/Program Files/R/R-3.4.1/bin/i386/Rgui.exe")
r("source('C:/Users/(my name)/Desktop/(folder)/hello.r')")
This causes RGui to open up with a blank R Console. And then nothing happens. When I click back to Python, the console shows Python is busy until I click the halt button, whereupon I get "OSError: [Errno 22] Invalid argument
What is the correct way to execute hello.r?
Thank you
Looks like I got it. This works:
r=pyper.R(RCMD="C:/Program Files/R/R-3.4.1/bin/R")
r.run("source('C:/Users/(my name)/Desktop/(folder)/hello.r')")
I am using Jupyter Notebook 5.6.0, in Python 2.7, on Windows 7. I have created %HOMEPATH%.jupyter\custom\custom.js file and added the following code to turn off autosave.
define([
'base/js/events'
],
function(IPython, events) {
events.on("notebook_loaded.Notebook",
function () {
IPython.notebook.set_autosave_interval(0); // Disable autosave
}
// Additional events.on() statements here
);
}
);
Unfortunately, it has no effect on autosave. However, the problem is not with the code (which one can find in various places), but the fact that Notebook does seem to even read custom.js, neither when the Notebook server starts nor when I open a ipynb file. Further actions I did:
1) I verified that it can find and read it using the appropriate Python code.
2) I added an alert() message in custom.js which is never shown
3) I added the following as an alternative for turning autosave off: Jupyter.notebook.set_autosave_interval(0); that I saw in some other post in stackoverflow.com.
Of course, none of these will have any effect if Notebook never reads custom.js. Any idea why is that happening?
... One day later:
I cleaned custom.js leaving only Jupyter.notebook.set_autosave_interval(0); Autosave is finally turned off! (So, most probably Notebook didn't like something in custom.js and rejected it totally!)
So I consider this question solved.
I recently switched to Visual Studio Code and I have to say I love it so far.
I'm working on a Python project, which includes the pip packages pylint and autopep8 and I configured VSCode to format the code according to these packages.
Only problem is: In the Python project I'm working on the line length is 100. So all my code looks like this:
The error says: E501:line too long (97 > 79 characters). Here are my VSCode settings:
{
"python.pythonPath": "~/.envs/myProject/bin/python",
"python.linting.pep8Enabled": true,
"python.linting.pylintPath": "~/.envs/myProject/bin/pylint",
"python.linting.pylintArgs": ["--load-plugins", "pylint_django", "--max-line-length=100"],
"python.formatting.autopep8Args": ["--max-line-length=100"],
"python.linting.pylintEnabled": true,
"files.exclude": {
"**/.git": true,
"**/.svn": true,
"**/.hg": true,
"**/CVS": true,
"**/.DS_Store": true,
".vscode": true,
"**/*.pyc": true
}
}
These settings at least now ensure that format on save keeps the lines at 100 max and does not wrap all my files lines to 79. Still it would be awesome without the warnings.
How do I disable these linter warnings?
I figured out how to do this. Add this line to your settings:
"python.linting.pep8Args": ["--max-line-length=100"],
For pycodestyle in Vscode 1.15.1:
"python.linting.pycodestyleArgs": ["--max-line-length=100"],
As of 2021 (Pylint reference), add this to your .vscode/settings.json file:
"python.linting.pylintArgs": ["--max-line-length=100"]
2020 version:
Add the following entry to your settings.json file
"python.linting.pylintArgs": ["-d", "C0301"],
If you haven't disabled these errors, and encounter a line too long warning, under "Problems" in VSCode, or by hovering over the underlined error, you will see VSCode say something along the lines of:
Line too long (188/100)Pylint(C0301:line-too-long)
As you can see, the value C0301 comes directly from this warning message. You can additionally disable other warnings by their error code as well.
For me the following worked. (VSCode version == 1.73.0)
Go to Settings.
Search "pylint".
Scroll down to "Pylint : Args"
Click "Add Item"
Type this in, --max-line-length=200
Adjust the max-line-length to your desired length.
I was having difficulty converting a program I made to a cgi script. I suspected it was to do with os.walk so I made a smaller test script to test this.
(I noticed the single \ before the D in the variable loc and tried changing that to a double \ still no change)
Produces no errors cant tell why it doesn't run the for loop with os.walk in the browser.
I tried adding some data into s and run for loop printing of contents of it and that worked fine, but trying to do it on os.walk I can't seem to get it to work. I can't find anything relating to the issue on google or stackoverflow.
Below is the code:
import cgi,cgitb,os
loc = "C:\\Users\\wen\Desktop\\sample data\\old py stuff\\"
cgitb.enable(display=1,logdir=loc)
s = []
print("Content-type:text/html\r\n\r\n")
print("<html>")
print("<body>")
print("<p>"+loc+"</p>")
for r,ds,fs in os.walk(loc):
print("<p>omgwtf</p>")
for f in fs:
s.append(f)
for i in s:
print("<p>"+i+"</p>")
print("</body>")
print("</html>")
Took a screenshot, the output in interpreter on the left and browser on right
i.imgur.com/136y1Yq.jpg
webserver is running iis7
I'm pretty sure I've solved the problem, I needed to give the folders permissions for 'Authenticated users'.