This question already has answers here:
How to drop into REPL (Read, Eval, Print, Loop) from Python code
(7 answers)
Closed 6 years ago.
How would you make the IDLE shell, using nothing but Python code? Now, I understand the beggingins, like a simple exec(raw_input('>> ')), but how would you get if and else statments, or for and while loops to work?
while 1==1:
is considered invalid syntax. How would you prevent that?
The suggested thread isn't nearly the same thing, as all of the answers but one answer how to run a python program from a python program. One of the answers points kinda of like what I'm asking, but it would still fail in an if and else statement, or a while or for loop.
The source code for IDLE is in the Python standard library, under the idlelib package. You can look at the source to see how they implement everything.
idlelib is a lot of code, so it might be overwhelming to try to go through it. The code module provides a Python implementation of Python's interactive mode; you could go through the source code for that to get an idea of how you could do things.
Related
This question already has answers here:
How to step through Python code to help debug issues?
(15 answers)
Closed 3 years ago.
I am trying to learn the basics of programming using python, and I am getting to the point where I have to deal with if statements and for loops. The thing is that I am getting a bit confused as to how my code is running. Is there a way to see how my code is being executed line by line; I feel that that would deepen my understanding of why code has to be written a certain way in order for computers to understand what I am trying to accomplish. I dont know if my question makes any sense to you guys, but I hope you can help me. Thanks a lot in advance.
You can use an interactive debugger. Some Python IDEs provide it.
You may want to give VS Code a try. Take a look at the debugger part of the tutorial.
What you are looking for is called a debugger. You can find some online, for example here: https://www.onlinegdb.com/online_python_debugger . Alternatively you can use pdb, the python debugger (the above link is just a friendly wrapper around it), directly from the command line, but if you are new to python and programming I wouldn't recommand it.
Try pdb.
python -m pdb script.py
More information on How to step through Python code to help debug issues?.
pdb documentation here.
This question already has answers here:
How do I script a "yes" response for installing programs?
(6 answers)
Closed 5 years ago.
I've come across a program run by Python2, which asks many questions of yes/no type.
I want to answer "yes" to all of them, but it's really difficult because there are literally hundreds of them (it's basically a parser of code, which asks about every found variable).
So, is there any possibility, how to force Python answer "yes" automatically?
I think about something similar to apt-get -y install. And I'm interested in answers about both Python2 and Python3.
I'm using Lubuntu 16.10 and my default Shell is GNU bash, version 4.3.48.
Try using yes:
yes | python ./script.py
If you have a more complex state to manage during interaction, there is also expect.
yes emits y by default, but you can customize it by providing an argument (e.g. yes yes), thanks #tobias_k. If you need a portable way (in Python), go with the suggestion from Jean-François Fabre (or just hack the script).
This question already has answers here:
How do I get time of a Python program's execution?
(38 answers)
Closed 6 years ago.
How do I check how long it takes for my code to execute. Is there an inbuilt way in python. Or is there some hidden tool in my IDE PyCharm that let's me do so.
You could try with
cProfile
in this way:
import cProfile
def myFunc():
...
cProfile.run('myFunc()')
Hope this solves your question!
So profiler is already builtin tool in Pycharm, it uses cProfiler by default if you don't have yappi installed. Here is the link to it PyCharm profiler.
And if you want profile your code without any attachment to PyCharm, check that SO question How can you profile a Python script?
This question already has answers here:
Undecompilable Python
(2 answers)
Closed 9 years ago.
Can I block others from seeing my code? I'm working on something in Python. As I save it, it's a (.py) and everyone can right-click and see the code. This is unlike C where I generate an .exe that you can't simply (!) read.
You can read the assembly code generated by the process of the compilation in C. The code is not hidden : it's a bit more complex to read, because it is ASM. But it is totally and definitively not hidden :)
Python is made by Open Source community, so the idea is not about hiding your code. If ever you want to make a .exe from a Python code, you can have a try to Py2Exe or Freeze :
http://www.py2exe.org/
http://wiki.python.org/moin/Freeze
Cheers,
K.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why do people write #!/usr/bin/env python on the first line of a Python script?
I am currently teaching myself Python and I have a question about python. In many examples of Python, I've seen the same line of code at the top:
#!/usr/bin/env python
My question is what purpose does the code above serve, and why is it necessary? So far, I've been practicing Python code without this line of code. If someone could explain this to me, it would help a lot. Thanks in advance.
#!/usr/local/bin/env python
You are specifying the location to the python executable in your machine, that the rest of the script needs to be interpreted with.
You are pointing to python is located at /usr/local/bin/python.
Python is not always installed at '/usr/local/bin/python'. You can also call different python versions by #!/path/to/your/python/version.
It is describing the location of the python install that your code should use. See here and here.