I am new to python and pycharm.
I am writing a program that takes years to run, so I want to see the iteration number while debugging.
this is my code:
import urllib2
for i in range(n):
print i
responses[i]=u2.urlopen(urls[i])
(I have an array of n urls)
so, when I run it I see the outputs:
0
1
2
3
etc
but when I am debugging I don't see the output.
any idea anyone?
Its a simple code block and there should not be any problem with Pycharm debugger. I suggest to use latest PyCharm version 5.0.
Right click on your python file and select 'Debug yourPythonFile.py'.It should print value of i.
Just a small suggestion, use (responses.append(u2.urlopen(urls[i])) instead of responses[i]=u2.urlopen(urls[i])
Related
I have Python 3.8(32-bit) installed, and I am using Atom to write my attempts and then copy-pasting them into the Python terminal.
The following code is copied directly from the very beginning of an introductory Python course I am taking "for fun":
n = 5
while n > 0:
print(n)
n=n-1
print('Blastoff!')
The code works in every sandbox I can find, and the last line works on its own in my terminal. But when I copy it to my terminal, I get an invalid syntax error that points to the word print. I can fix this and get the desired output by changing my code to:
n = 5
while n > 0:
print(n)
n=n-1
else:
print('Blastoff!')
But I have three issues with this:
Why does my original code not work, as it is copied directly from the course?
I need to hit Enter twice after copying in that second block of code for it to run. Why is that?
Why does Atom insist on indenting the last print farther than my other indents?
Here is what I am seeing when entering my first code brick:
>>> n = 5
>>> while n > 0:
... print(n)
... n=n-1
... print('Blastoff!')
File "<stdin>", line 4
print('Blastoff!')
^
SyntaxError: invalid syntax
Since you're entering code into the Python interpreter, it'll interpret the code line-by-line. This is great for quick tests and checks, but for larger code, you'll want to run an entire file.
You can achieve this a couple different ways:
Running it from the command prompt/terminal. If your Python executable is in your PATH, you can open a command prompt and navigate to your file and run python myfile.py. See "How to add Python to Windows PATH".
If you've installed Python from python.org, you may have IDLE installed. You can run the IDLE application and open your file from the menu File > Open. From there, you can run the file from the menu Run > Run Module.
I'd suggest the second option since you are learning and it will help you focus on coding instead of fighting your code environment. However, feel free to revisit option #1 in the future. It is definitely helpful to know your way around the command line (if you work on a machine without IDLE installed, this would be the proper way to run Python files).
Also "How to Run Your Python Scripts" is a great resource for learning more about how running scripts in Python works.
How do you debug the code in the PyCharm IDE when it requires console input? For example, I have a piece of code,
# if the config already exists prompt what to do
if pc and not self.prompt.ask_yesno('project_ovverride'):
self.prompt.say('setup_abort')
return
This breaks in the line highlighted and I wasn't able to proceed for not being able to provide the console input. At the moment, I comment it out, but, there might be a way to provide the required console input as well.
Thank you.
If you're simply looking to input via CLI while debugging; you could simply use step into as shown below. - Enable 'Run with console' under your run configuration first.
Change tab to console in your debugger.
Click on step into until you see the question in the console.
Input your answer as needed.
Click on continue or any other action from your debugger as needs be.
If you'd like to debug through running a script in CLI you're looking for something on the lines of pdb (Python Debugger). You can read more here.
Example:
my_example.py
try:
pdb_test = 1 / 0
except ZeroDivisionError:
print('Argh stop it!')
Command Line:
(venv) $ python3 -m pdb my_example.py
> /my_example.py(1)<module>()
-> try:
(Pdb) s
> /my_example.py(2)<module>()
-> a = 1 / 0
(Pdb) s
ZeroDivisionError: division by zero
> /my_example.py(2)<module>()
-> a = 1 / 0
(Pdb)
What the above is showing is merely me using s to command the pdb to step - in the documentation you can find all the commands you might want to use including continue et cetera.
Initially, we need to set the Run with Python console in the Run configuration of the PyCharm IDE and then, we can change the debugger window to the console window at the time of debugging the software. I provided the screenshots that illustrate the formula,
Now, switch from the debugger to the console and provide the desired input.
If you're using Pycharm 2018.3 or above you can redirect your input to a file.
PS. I haven't tried this but it should work fine.
I am using Pycharm 2018.3.4. When python 3 is used, it indicates error for the end="" in the print function.
However, the code could still be run without any problem. So how to remove the red line for syntax check?
(I feel pycharm gets more popularity in stackoverflow than superuser, so I asked the question here)
Check your Interpreter's version is python 3.x in the right bottom of the pycharm, weather you are 3.x, You could try to retry the "InterpreterSetting" , this buttom.
I am not English speaker, I was left a comment by my language, but it was deleted by someone.
enter image description here
I have just started learning Python. I've been using R to do data analysis. One of the good things about RStudio is that we can select a few statements and press Ctrl+Enter to execute those lines. RStudio would show the output for each line.
For instance, the output of these lines:
a<-2
b<-3
a+1
b+2
would be
> a<-2
> b<-3
> a+1
[1] 3
> b+2
[1] 5
Please note that R, by default, would print all lines--not just the last line. This is immensely helpful when executing long scripts.
I learned from another thread and this thread that we can simulate above in Python by adding the following statements:
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
This works well. For instance, here's input and output:
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
from fractions import Fraction
import fractions
z = fractions.Fraction(1,3)
x = Fraction(1,3)
y = Fraction(2,3)
Fraction('0.25')
x+y
Out[50]: Fraction(1, 4)
Out[50]: Fraction(1, 1)
I have one question:
a) Adding InteractiveShell.ast_node_interactivity every time I execute my code is really annoying. Is there any way I can make this auto-executable (like .RProfile file in RStudio) when I start my PyCharm? (In RStudio, we have .RProfile file in which we can add all customizations. I prefer using PyCharm because of a lot of inbuilt features.)
One of the recommendations from SO threads was to modify ..\.ipython\profile_default and add above lines (i.e. ...ast_node_interactivity...) in .py file in Windows. Unfortunately, I have added above statements, and it wouldn't print the output of all statements in PyCharm. I still have to add those two lines to every .py file in PyCharm to see what's happening in my script. I am extremely frustrated with this.
Another recommendation was to add ;. This is annoying to me as well because then I have separate, say, 50 lines by ;, which would be difficult to read. Also, one has to remember to add ;, which is not good.
I am an absolute beginner. So, I'd appreciate any thoughts. I have googled this topic as well, and it seems there isn't much discussion on this topic.
I am adding clarification: I want to use PyCharm's Execute Selection in Console which has keyboard shortcut Alt + Shift + E in Windows. One would have to select a bunch of line of code and then see the output for each line (just as RStudio does). This helps when debugging a long script. As it is, it only prints the output of the last line if I don't add above two lines.
I wrote the following code in Python IDLE, using its editor
import urllib.request
print(urllib.request.urlopen('https://github.com').read().decode('utf-8'))
and then saved the code as a script. After I ran the script, Python shell displayed the page source I want.
When I changed the above code to:
import urllib.request
urllib.request.urlopen('https://github.com').read().decode('utf-8')
and then ran the script, Python shell displayed nothing. This is understandable.
The weird thing for me, however, is that if I run the above code (the one without print) interactively in a Python shell, Python shell can still display the page source, as you see:
I don't understand why.
I use Python 3.6.3.
You are using the interactive python prompt. It automatically prints return values for you so that you can see the result of what you're doing.
Try typing 3 + 2. You know this doesn't print anything either - but you will see the result.
Likewise, if you put those two lines into a file you won't get any output.
It's because the shell is printing the result of execution. If you assign the function call to a variable then it will suppress the printing.
i.e. html = urllib.request.urlopen( ... )