python programming query - python

I'm new to Python programming.
My question is where to write python programs in linux--
in terminal or
any writing pad like gedit etc
Should we write one program again and again for running or we should call the program and run it.

After you install Python (it's installed by default on most Linux-es), you have two options:
Terminal. Just type python <ENTER> and you will be taken to the Python interpreter interactive prompt. For anything but the most basic stuff, however, you may want to intall IPython - an alternative interactive prompt that's much better than the default one.
In a file. Save your code into a .py file and run it with python myfile.py
But first and foremost, start by learning Python - the official tutorial is a great place to start. Among other useful information, its chapter 2 discusses how to use the Python interpreter for beginners.
Stackoverflow also has a lot of great resources for learning Python. This question, for example, and many others.

If you are learning, or you are evaluating expressions, you could run Python in terminal, or use IDLE.
But if you are writing large chunks of code, then you should consider using an IDE.
You could either use Geany, or use Eclipse with PyDev. I prefer Eclipse myself.
For running, you can run it using the command python program.py, or just add the line
#!/bin/python
to the beginning of your program, grant it execution permission using chmod, and run it with ./program.py command.

Related

Does a "terminal" always need to run with python

Hi im a bit new to python but i want to learn more my question is when you build a web application and you are going to use python to do the data handling and calculations does this mean in order for that to be used a terminal,in this case lets say on windows will have to run and that basically listens if and when something was triggered or executed on the python program/script
This article summarizes how different types of Python run. By default, on many systems, CPython is present. When it interprets py files initially, it may create pyc files, which can then run on the CPython virtual machine, described here. The virtual machine and interpreter are also running when you run python on your terminal, if you are using this. As described in the first article however, CPython isn't the only way to run Python.

In Visual Studio Code, how do I load my python code to a read-print-eval loop?

I am teaching a class that uses VScode.
I am used to teaching using IDLE, and it is very nice for the students to be able to call their defined functions and run snippets of code in a python terminal, for debugging purposes.
In VScode, they I have been unable to do the same in a satisfactory way.
Option1: I can select all code, right click and run selection/line on terminal. This works for small snippets, but I cannot do it for the whole file (even after selecting the whole file with ctrl-A). On linux, this works, but on windows, it does not, unfortunately (and my students use windows)
Option2: I can use the debug console. This requires adding a breakpoint in one of the last lines of the file, and does not offer tab completion. It works, but is less convenient than IDLE.
Option 3: I can also add the commands to run to the bottom of the file (which is a least preferred alternative, given that is forgoes the interativity of the read-print-eval loop).
Is there any better solution? Installing a VScode extension would not be a problem.
Visual Code is just a text editor like your traditional notepad. to run and debug any kind program you need to install the particular extension for the programming language.
In your case you are using python so you need to install the extension of it. the best one is the "Python" which is developed by microsoft itself. go to your extensions manager and install this extension. right click and click "run python file in terminal" and you are all set.
this will run exactly as they run from the idle(which is default IDE provided by python itself) you can enter the arguments from the console itself. according to me this is the best way to run and debug python programs in VScode.
another way is that VScode shows which python version is installed on your computer on the left bottom side, click on it and the programs will use this interpreter.
out of all the ways listed here and many others, the best method is to run the program in the terminal which is the recommend by python itself and many other programmers.
this method is very simple. what you have to do is open up your command prompt and type the path where python.exe is installed and the type the path of the your program as the argument and press enter. you are done !
ex : C:\Python27\python.exe C:\Users\Username\Desktop\my_python_script.py
You can also pass your arguments of your program in the command prompt itself.
if you do not want to type all this and then just use the solution mentioned above.
hope that your query is solved.
regards

Calling to python interactive script from another script

I am using Python 2.7
I have interactive Python script, named A, written using Cmd module.
However, sometimes there is a need to activate script A from other Python scripts.
It also could be nice if the interactive script A wouldn't exit in between commands given to them.
I tried to search for solutions and I found couple here
One is to pass a file from which the interactive script will read commands. It works well for me but it is not much liked.
Another is to use echo which doesn't work for me (still trying).
I wonder whether there are other solutions.....

Learning Python the Hard Way terminal setup

So I'm trying to setup the terminal in conjunction with Notepad++ and I'm a little confused. I've managed to download Python 2.7 and load it up in Powershell but I don't really know where to go from there. Do I even need to use a terminal? I know it says you can't use IDLE but it would be a lot easier. If anyone can guide me through getting the right setup to go on with the book I'd appreciate it a lot.
If you managed to get python running in the powershell following the Exercice 0, and you read the "Warning For Beginners" section, you can proceed to Exercice 1.
Why do they recommend that setup? Notepad++ is on Windows a powerful text editor very similar to other text editors, so you will soon get familiar with it. You will need to save your scripts in files and type a special line in the powershell to run and test them. That way you will get accustomed to running programs with arguments and if you make a failure, you can edit your code, save it and run it again.
In IDLE, you are already in a Python environment, so you cannot give arguments to your program inside it (actually you can but it's complex). You can also get an editor, save your file and run the script but also here it's a harder and less obvious way than Notepad++. When you get comfortable with the environment in that book you may change to get something that suits better your way of doing.

How can I save / copy classes & functions I've written in the python interpreter?

How can you save functions/ classes you've writing in a python interactive session to a file? Specifically, is there a way in pydev / eclipse's interactive session (on a mac) to do this?
I just started learning python - and am enjoying using the interpreter's interactive session for testing and playing with modules I've written. However, I find myself writing functions in the interpreter, which I think, oh it would be cool to save that to my script files. How do I do this?
I tried:
import pickle
pickle.dump(my_function, open("output.p", "w"))
But it seems to be more of a binary serialization, or at least nothing that I could copy and paste into my code...
Are there ways to see the code behind classes & functions I've defined in the interpreter? And then copy them out of the interpreter?
Update:
Ok, here's what I've learned so far:
I missed the easiest of all - PyDev's interactive session in eclipse allows you to right click and save your session. Still have to remove >>>'s, but gets the job done.
IPython is apparently the way to go to do this.
How to save a Python interactive session? has more details.
The best environment for interactive coding sessions has to be IPython, in my opinion. It's built on and extends the basic Python interpreter with a lot of magic, including history. For example, you can issue the command %logstart to dump all subsequent input to a file, which still needs to be edited afterward before it will be a script, but gives you a lot to work with.
When installing IPython, don't forget pyreadline.
In general, however, it is best to write code in an IDE and then run it. IPython helps here as well. If you write and save the script, then use the IPython "run" command to run it, the entire global namespace of the script will be available for inspection in your IPython session. Additionally, you can use the -d argument to run to trigger the pdb debugger immediately on any unhandled exception.
If you're more of a straightlaced IDE and debugger kind of guy, then the easiest and best lightweight environment has to be PyScripter.
I think the answer is to change your workflow.
What I do is write my functions in an editor (emacs), and then press a key combination (Ctrl-c Ctrl-e) to send the region of text to the (i)python interpreter.
That way I can save the function if I want, and also play with it in an interpreter.
Emacs is central to how I do it, but I'm sure there must be similar approaches with many editors (vim, gedit, etc) and IDEs.
PS. Finding a good editor is crucial when working with Python. The editor must be able to move blocks of code to the left and right easily, or the whitespace issue becomes too onerous.
I dislike typing blocks of code in the python interpreter because it doesn't allow me to shift blocks easily. You'll like Python even more when you find the right editor.
You can setup a python history file which stores everything you type into the interpreter.
Here's how:
http://docs.python.org/tutorial/interactive.html
I think it can't be done.
Python can perform instrospection with the inspect module, but the inspect.getsource function won't work without a source file.

Categories

Resources