I am a newbie trying to use Python (2.17.15 via Anaconda) on Visual Stodio Code on my Mac. I have the following simple code:
def function(x):
y = x + 2
return y
This code is giving me the usual trouble, an indentation error:
return y
^
IndentationError: unexpected indent
>>> return y
File "<stdin>", line 1
return y
^
IndentationError: unexpected indent
>>>
Needless to say that Jupyter or Spyder have no problem with this. I checked that on VSC tab gives 4 spaces. All similar questions are related to this, but I cannot fix it.
Other, built in functions of Python work fine.
Please give me some help or tips since I do not know how to escape this.
UPDATE
Installing again Python3 this simple code DOES work on Sublime but still not on VS Code. I still get the same error in VS Code.
UPDATE2
So, another update. If I change from return to print and instead of shift-command debug and run the code then it works.
Any idea what is going on?
This looks like it's because you're running the code with Shift+ENTER.
VS Code has the following 2 bindings for Shift_ENTER:
I believe that you're seeing the 2nd of these, which says "Run Selection/Line in Python Terminal. I suspect you have the focus on the return y line, and so it's only running that single line of code.
If, instead of Shift+ENTER, you use the Run Code command in VS Code, you should see it work fine:
You might well think "OK...so if I select all of the code this will work, right?" and I agree...this feels like it should work. However, I see a similar issue. I'll see if I can work out why, but for the moment you can use the Run Code command in VS Code and that will do what you want. If you highlight the code you want to run, that will limit what gets executed.
Run Code can be executed with Ctrl+Alt+N
It looks like this issue (that selected code doesn't run correctly with Shift+ENTER) is a bug that's being tracked by here: https://github.com/Microsoft/vscode-python/issues/2837
And a work around (not ideal) is to add code before/after your function that is NOT indented, and then select and execute those lines too:
print("this...")
def function(x):
y = x + 2
return y
print("...now works if you select all these lines and Shift+ENTER!")
This is a bug from the python extension, which you need to run chunks of code in the interactive mode.
So in the example code below:
for lastRun in list(d_RunPanelsPresent.keys()):
# some indented commands
logFile = f"/nexusb/Novaseq/{lastRun}/logPPscript.txt"
if not os.path.isfile(logFile):
with open(logFile, 'w+') as f:
pass
else:
pass
If I highlight as follows (note where the cursor is):
I will get an error.
The solution is to highlight the code from the very left of the code editor, as shown below:
This works for me 100% of the time.
Forgetting a semicolon at the end of the function definition produces the same error.
Related
def greet(name):
"""
This function greets to
the person passed in as
a parameter
"""
print("Hello, " + name + ". Good morning!")
This is a basic example for defining a function, and when I run this code on Python 3.8.5 by VSC, the result is like below.
>>> def greet(name):
...
File "<stdin>", line 2
^
IndentationError: expected an indented block
It says that there is an indentation error but I really don't know why. And also, line 2 is not just blank but according to the result, line 2 seems like blank.
for number in range(5):
print("Thank you")
>>> for number in range(5):
...
File "<stdin>", line 2
^
IndentationError: expected an indented block
Same error for 'for'. Can anyone help?
Right now, you are running the Python script line-by-line by placing the VS Code cursor on each line of code and then pressing shift+enter. This utilizes the Python Interactive window afaik. It works fine for running individual lines but if you want to run a collection of lines that include indentation e.g. a for loop or function definition, then select all of those lines together and then press shift+enter. You would need to do this for code that's indented, for example:
def greet(name):
print("Hello, " + name + ". Good morning!")
Alternatively, if you have read Getting Started with Python in VS Code and specifically installed the Python extension for VS Code then you will be able to click the Run Python File in Terminal button (it looks like a Play icon) to run your Python script.
Alternatively, you can launch a Terminal Window in VS Code and manually run the Python script using python3 hello.py (or potentially python hello.py, depending on your Python installation).
You should not press Shift+Enter to run the code in the terminal line by line.
Because the REPL will add an Enter to start a new line automatically like this:
You can select all of the codes then send them to the REPL:
I tried running the below code but VS Code is showing syntax error. I checked on internet and notes but found the loop is fine.
i = 1
while i <= 5:
print(i)
i = i + 1
While loop showing syntax error
I don't think there is anything wrong with your code but you should try to create a new folder preferably outside of Appdata. or One drive folder
There isn't anything wrong with your actual code. When I run it it executes as you would expect. I think the problem must be the way you are executing the program. I think you are attempting to run it in the python interpreter. Where it says "2:Python" you want it to be like "cmd" or "Code" or something and then you can just type in python loop.py maybe try clicking the plus next to it or select the first option in the dropdown.
I am a newbie trying to use Python (2.17.15 via Anaconda) on Visual Stodio Code on my Mac. I have the following simple code:
def function(x):
y = x + 2
return y
This code is giving me the usual trouble, an indentation error:
return y
^
IndentationError: unexpected indent
>>> return y
File "<stdin>", line 1
return y
^
IndentationError: unexpected indent
>>>
Needless to say that Jupyter or Spyder have no problem with this. I checked that on VSC tab gives 4 spaces. All similar questions are related to this, but I cannot fix it.
Other, built in functions of Python work fine.
Please give me some help or tips since I do not know how to escape this.
UPDATE
Installing again Python3 this simple code DOES work on Sublime but still not on VS Code. I still get the same error in VS Code.
UPDATE2
So, another update. If I change from return to print and instead of shift-command debug and run the code then it works.
Any idea what is going on?
This looks like it's because you're running the code with Shift+ENTER.
VS Code has the following 2 bindings for Shift_ENTER:
I believe that you're seeing the 2nd of these, which says "Run Selection/Line in Python Terminal. I suspect you have the focus on the return y line, and so it's only running that single line of code.
If, instead of Shift+ENTER, you use the Run Code command in VS Code, you should see it work fine:
You might well think "OK...so if I select all of the code this will work, right?" and I agree...this feels like it should work. However, I see a similar issue. I'll see if I can work out why, but for the moment you can use the Run Code command in VS Code and that will do what you want. If you highlight the code you want to run, that will limit what gets executed.
Run Code can be executed with Ctrl+Alt+N
It looks like this issue (that selected code doesn't run correctly with Shift+ENTER) is a bug that's being tracked by here: https://github.com/Microsoft/vscode-python/issues/2837
And a work around (not ideal) is to add code before/after your function that is NOT indented, and then select and execute those lines too:
print("this...")
def function(x):
y = x + 2
return y
print("...now works if you select all these lines and Shift+ENTER!")
This is a bug from the python extension, which you need to run chunks of code in the interactive mode.
So in the example code below:
for lastRun in list(d_RunPanelsPresent.keys()):
# some indented commands
logFile = f"/nexusb/Novaseq/{lastRun}/logPPscript.txt"
if not os.path.isfile(logFile):
with open(logFile, 'w+') as f:
pass
else:
pass
If I highlight as follows (note where the cursor is):
I will get an error.
The solution is to highlight the code from the very left of the code editor, as shown below:
This works for me 100% of the time.
Forgetting a semicolon at the end of the function definition produces the same error.
I am new to PyCharm and I have 'Process finished with exit code 0' instead of getting (683, 11) as a result (please see attachment), could you guys help me out please? Much appreciate it!
That is good news! It means that there is no error with your code. You have run it right through and there is nothing wrong with it. Pycharm returns 0 when it has found no errors (plus any output you give it) and returns 1 as well as an error message when it encounters errors.
Editors and scripts do not behave like the interactive terminal, when you run a function it does not automatically show the the result. You need to actually tell it to do it yourself.
Generally you just print the results.
If you use print(data.shape) it should return what you expect with the success message Process finished with exit code 0.
exit code 0 means you code run with no error.
Let's give a error code for example(clearly in the below image): in below code, the variable lst is an empty list,
but we get the 5 member in it(which not exists), so the program throws IndexError, and exit 1 which means there is error with the code.
You can also define exit code for analysis, for example:
ERROR_USERNAME, ERROR_PASSWORD, RIGHT_CODE = 683, 11, 0
right_name, right_password = 'xy', 'xy'
name, password = 'xy', 'wrong_password'
if name != right_name:
exit(ERROR_USERNAME)
if password != right_password:
exit(ERROR_PASSWORD)
exit(RIGHT_CODE)
I would recommend you to read up onexit codes.
exit 0 means no error.
exit 1 means there is some error in your code.
This is not pyCharm or python specific. This is a very common practice in most of the programming languages. Where exit 0 means the successful execution of the program and a non zero exit code indicates an error.
Almost all the program(C++/python/java..) return 0 if it runs successful.That isn't specific to pycharm or python.
In program there is no need to invoke exit function explicitly when it runs success it invoke exit(0) by default, invoke exit(not_zero_num) when runs failed.
You can also invoke exit function with different code(num) for analysis.
You can also see https://en.wikipedia.org/wiki/Exit_(system_call) for more details.
What worked for me when this happened was to go to
Run --> Edit Configurations --> Execution --> check the box Run with
Python Console (which was unchecked).
This means that the compilation was successful (no errors). PyCharm and command prompt (Windows OS), terminal (Ubuntu) don't work the same way. PyCharm is an editor and if you want to print something, you explicitly have to write the print statement:
print(whatever_you_want_to_print)
In your case,
print(data.shape)
I think there's no problem in your code and you could find your print results (and other outputs) in the tab 5: Debug rather than 4: Run.
I just ran into this, but couldn't even run a simple print('hello world') function.
Turns out Comodo's Firewall was stopping the script from printing. This is a pretty easy fix by deleting Python out of the Settings > Advanced > Script Analysis portion of Comodo.
Good Luck
I had same problem with yours. And I finally solve it
I see you are trying to run code "Kaggle - BreastCancer.py"
but your pycharm try to run "Breast.py" instead of your code.
(I think Breast.py only contains functions so pycharm can run without showing any result)
Check on tab [Run] which code you are trying to run.
Your starting the program's run from a different file than you have open there. In Run (alt+shift+F10), set the python file you would like to run or debug.
I am trying to write a program to find the sum of a list using recursion in python and my code is this
value = 0
def sum_list(alist):
global value
if len(alist) == 0:
return value
value += alist.pop()
return sum_list(alist)
print sum_list(range(10))
But when i am executing this script I am getting a weired error.
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/%{ <-- HERE (.*?)}/ at /usr/bin/print line 528.
Error: no "print" mailcap rules found for type "text/x-python"
I searched for it but couldn't get why this error is coming.
Help will be appreciated
Your code is valid Python 2. (It's not valid Python 3 because the print statement would have to be different.)
You don't say how you are actually running this code, but it looks like it is not actually being interpreted as a Python program. The error message is coming from /usr/bin/print, so I think you have managed to get this interpreted as a shell script somehow, and the "print" on your final line is running /usr/bin/print. That's obviously not what you want.
If you are executing this inside a source file, try putting the following line at the top of it, to tell the shell to run this as a Python program:
#!/usr/bin/env python
Alternatively, run it using
python myfile.py
I had this problem, it was an oversight at the terminal prompt. I was typing print instead of python
$ print sum.py
$ Error: no "print" mailcap rules found for type "text/x-python"
Correct way:
$ python sum.py
$ program runs successfully...
The error message comes from Perl. No idea how you managed to call it with this Python code.
see also http://www.perlmonks.org/?node_id=113525