Command Prompt Python - python

What is the benefit of running code through the command prompt/terminal vs an ide?
I've noticed recently when using the progressbar module of python that the progress text is updated on the same line in the command prompt window while the ide prints each text on the next line. Why are these different? Are they not running though the same interpreter?

The IDE adds an extra layer of software between the program and the python interpreter.
What you are seeing is probably that the IDE's output window is not a complete terminal emulator, and doesn't understand or ignores the commands that the progressbar module uses. to keep the output on the same line.
Have a look at ipython. It is a very nice environment for testing and running python code.

Each IDE is infact interacting via the command line and redirecting streams into it's implementation of showing those outputs, Each IDE has it's own way of doing this, command prompt is more powerful if you are expeirienced and easy to try one off scripts, try ipython which is great for beginners and learners alike for fast access to the programming environment and trying out module.

Related

Open Python interactive session and editor

I am looking for some pieces of advices in order to accomplish a tiny task regarding Python. If someone would ask to provide a pic of a 'started interactive session of Python with your favorite editor with a Python script', what would you show to this person? Should it be a void script? How do you interpreter 'started interactive session'? How about your own favorite editor (I mean that you would suggest for Windows 10)?
Sorry for the triviality of my question,but I have just started with beginners' Python course
Just to make sure I am on the right way, if I have to submit to someone else a started interactive session and your favourite editor with a Python script, will be it sufficient to show the following windows as in the picture?
If you want to do interactive things, you probably just want to use jupyter notebook: https://jupyter.org/install#jupyter-notebook
You can always just type python at your terminal prompt if you have python installed, this will start an interactive session in your terminal, but jupyter is definitely easier to use once you get it set up.
Edit: regarding favourite editor, this is very much opinionated but I love sublime text. https://www.sublimetext.com/
Note that you probably would use one or the other: you would use a text editor to write scripts that could be run in a terminal, for example
# hello_world.py
print("Hello, world!")
then in your terminal
python hello_world.py
whilst you would use a jupyter notebook for example for quick experimentation or for demonstrating usage of your software to others.
In terms of interactivity, you should really check out Jupyter Notebooks. It's industry standard for a lot of tasks, widely used and with great performance and support. Also, Jupyter has an in-built code editor than can be run via localhost in your browser.
However, for a code editor, I will never stop recommending VSCode. Huge game changer, light-weighted and with support for pretty much any language. Jupyter notebooks can be run directly from VSCode, and the latest features that Microsoft introduced in this regard make using Jupyter inside VSCode really easy and intuitive. Also, extensions are a positive point as well.
If you're only planning on programming with Python, maybe checking out PyCharm is also a good idea, but I have working mainly in Python for the past 3 years and I have never missed anything on VSCode, even though I've tried PyCharm several times.
The simplest answer: go with the IDLE IDE, it comes bundled with Python by default. It starts with an interactive session, so you just type at the prompt (>>>):
print ("Hello, world!")
and your task is done, if it prints out your "Hello, world!" line.
In general, you start your python interactive session (python REPL, python terminal, python console, there are several terms for the same thing) and, since it's an interpreted language, everything you type in, Python will interpret and execute, if valid. Nevertheless, except for atomic examples, you'll want to use a code editor or IDE.
As a beginner, I'd avoid jupyter for the time being, it could get cumbersome. Stick with the default IDLE editor (you open a new file and type away) until you feel confident, then make the switch toward some editor or IDE that you fancy - Visual Studio Code is popular and has python debugger, vim is old as Bible, can run on a calculator, but it's a modal editor, best used with touch typing, Notepad++ is also good for coding...

Python curses Redirection is not supported

I am trying to use Curses in PyDev in Eclipse in Win7.
I have installed Python 3.2 (64bit) and curses-2.2.win-amd64-py3.2. When I input the following testing codes into PyDev:
import curses
myscreen = curses.initscr()
myscreen.border(0)
myscreen.addstr(12, 25, "Python curses in action!")
myscreen.refresh()
myscreen.getch()
curses.endwin()
It did not show any syntax error, so I think the curses was installed correctly.
However, when I ran it as Python Run, the output showed: Redirection is not supported. I do not know where this problem comes from. I googled a lot but can't find related information.
Recent PyCharm versions (I am currently running 2017.2, not sure when this option was added, or if it has been there the entire time) have the option "Emulate terminal in output console". Curses works with this option checked.
You cannot expect to use curses with a non-terminal.
Probably you get this because you are running the script from inside an IDE, like PyCharm or any other.
All IDEs do provide consoles that are not terminals, so that's where the problem comes from.
For a Pycharm user the solution given by codeape works fine :
Snapshot
You can't use any IDE to run python files with the curses package. I used to run in pycharm and naturally couldn't run.
Change to the command line to run:
for testing follow my following steps
on desktop open notepad and copy paste the code and save it as filename.py
open command line change directory to desktop use below command cd Desktop and hit enter type python example.py and hit enter, your program will definitely run
My workaround is to create a Run Configuration that calls a curses script. The little overhead is worth not having to switch to the terminal and manually run the script hundreds of times a session. I use Intellij but I imagine the process should be similar in PyCharm.
The desired result is the convenience of a button to run the script:
First create a script that calls the entry script, for instance:
ptyhon name-of-script.py
Then, to create a configuration for each script:
Go to Edit configuration.
Click the plus button and add a Shell Script.
Enter the path to a shell script.
Here is a picture of a directory with a couple of sample scripts.
I use this process to view my progress. My curses scripts are very modest so fortunately I can live without a debugger.

Vim for python and R scripting (in Screen or not)

I've been trying to find this information online but I'm not getting the answer.
I've used RStudio and Geany for editing files before. Now I'm trying to use ViM to edit python and R files (I know there's RPy, but nothing to do with my problem).
I would like to know how can I have 3 terminals (could also be vim buffers, or screen windows) with one running ViM and the others running R and Python. When I execute a Python script, the terminal (window or buffer) with python shows the output. The same when I run R scripts.
I would appreciate insight on this as this is something that's keeping me from using ViM regularly. I would also consider a solution with terminator terminal multiplexer or guake terminal. Any information about sending code for scripting from one instance to another is welcome.
Are you looking for a way to have a REPL inside Vim? If so, Vim wasn't really designed with that in mind, though there are some plugins that try. Conque is an example.
Some things I use to have a quicker code/run/test iteration with Python:
IPython's %edit feature, which starts editing a script with $EDITOR and will run the script after you exit.
vim-ipython which can send/execute/recieve code via an IPython interpreter.
tmux which allows you to have multiple shells side by side, but with little interaction between them.
Vim-slime is a general-purpose solution to this I'm pretty happy about, it will send blocks of code to any tmux pane, meaning it works for any language.
https://github.com/jpalardy/vim-slime
Your requirements for online information may not have been spelled out in enough detail, since I seem to find a wealth of information on using ViM as an IDE for both R and Python:
R:
http://www.r-bloggers.com/r-with-vim/
http://www.vim.org/scripts/script.php?script_id=2628
http://www.vim.org/scripts/script.php?script_id=1048
Python:
http://wiki.python.org/moin/Vim
http://blog.dispatched.ch/2009/05/24/vim-as-python-ide/
http://dancingpenguinsoflight.com/2009/02/python-and-vim-make-your-own-ide/
Have a look at vim-ipython, a plug-in for Vim.
You need to download the source (linked above), and run the Vim command :source path/to/file/ipy.vim.
Start by running a new IPython session (e.g. using IPython qtconsole or IPython notebook) and then type :IPython into Vim. Your Vim is now connected to the IPython instance you just opened.
You can press F5 to run the whole python script in your Vim, or Ctrl+s to run the current line. Ctrl+s will also run whatever is selected if you're in visual (i.e. 'select') mode.

Running Python from the Windows Command Line

How do I run a Python file from the Windows Command Line (cmd.exe) so that I won't have to re-enter the code each time?
Wouldn't you simply save your Python code into a file, and then execute that file using Python?
Save your code into a file called Test.py.
And then run it?
$ C:\Python24\Python.exe C:\Temp\Test.py
If you don't want to install an IDE, you can also use IDLE which includes a Python editor and a console to test things out, this is part of the standard installation.
If you installed the python.org version, you will see an IDLE (Python GUI) in your start menu. I would recommend adding it to your Quick Launch or your desktop - whatever you are most familiar with. Then right-click on the shortcut you have created and change the "Start in" directory to your project directory or a place you can mess with, not the installation directory which is the default place and probably a bad idea.
When you double-click the shortcut it will launch IDLE, a console in which you can type in Python command and have history, completion, colours and so on. You can also start an editor to create a program file (like mentioned in the other posts). There is even a debugger.
If you saved your application in "test.py", you can start it from the editor itself. Or from the console with execfile("test.py"), import test (if that is a module), or finally from the debugger.
If you put the Python executable (python.exe) on your path, you can invoke your script using python script.py where script.py is the Python file that you want to execute.
Open a command prompt, by pressing Win+R and writing cmd in that , navigate to the script directory , and write : python script.py
A good tool to have is the IPython shell. Not only can it run your program (%run command), but it offers also many tools for using Python interactively in an efficient manner (automatic completion, syntax coloring, quick access to the documentation, good interaction with Matplotlib,…). After you install it, you'll have access to its shell in the Start menu.
You need to create environment variables. Follow the instructions here: http://www.voidspace.org.uk/python/articles/command_line.shtml#environment-variables
In DOS you can use edit to create/modify text files, then execute them by typing python [yourfile]

Cleanest way to run/debug python programs in windows

Python for Windows by default comes with IDLE, which is the barest-bones IDE I've ever encountered. For editing files, I'll stick to emacs, thank you very much.
However, I want to run programs in some other shell than the crappy windows command prompt, which can't be widened to more than 80 characters.
IDLE lets me run programs in it if I open the file, then hit F5 (to go Run-> Run Module). I would rather like to just "run" the command, rather than going through the rigmarole of closing the emacs file, loading the IDLE file, etc. A scan of google and the IDLE docs doesn't seem to give much help about using IDLE's shell but not it's IDE.
Any advice from the stack overflow guys? Ideally I'd either like
advice on running programs using IDLE's shell
advice on other ways to run python programs in windows outside of IDLE or "cmd".
Thanks,
/YGA
For an interactive interpreter, nothing beats IPython. It's superb. It's also free and open source. On Windows, you'll want to install the readline library. Instructions for that are on the IPython installation documentation.
Winpdb is my Python debugger of choice. It's free, open source, and cross platform (using wxWidgets for the GUI). I wrote a tutorial on how to use Winpdb to help get people started on using graphical debuggers.
You can easily widen the Windows console by doing the following:
click the icon for the console window in the upper right
select Properties from the menu
click the Layout tab
change the Window Size > Width to 140
This can also be saved universally by changing the Defaults on the menu.
Year 2017-2022 Answer:
Try Visual Studio Code, it has great support for Python debugging, auto completion and more!
See this link for details:
https://code.visualstudio.com/docs/languages/python#_debugging
However, I want to run programs in
some other shell than the crappy
windows command prompt, which can't be
widened to more than 80 characters.
Click on the system box (top-left) in the command prompt and click properties. In the layout tab you can set the width and height of the window and the width and height of the screen buffer. I recommend setting the screen buffer height to 9999 so you can scroll back through a long output.
I use eclipse with pydev. Eclipse can be sluggish, but I've become attached to integrated svn/cvs, block indent/unindent and run as unittest features. (Also has F5 run)
If your comfortable in emacs though I don't see any reason to make such a major change.
I suggest instead that you replace your 'crappy command prompt' with powershell. It's not as crappy.
(As mentioned by Soviut and The Dark - you can increase the buffer width/window size to more than 80 by title-bar>right-click>Properties>Buffer Width/Window Size edit even in crappy cmd)
Wing IDE is awesome. They also have a free version.
If you ever graduate to vim, you can just run the following command to start the program you're currently editing in an interactive shell:
:!python -i my_script.py
I edit my Python programs in EditPlus. I've configured a user tool that allows me to run my program and have its output appear in a frame below my editor window. My configuration will even let you double click on an exception lines in the output to jump directly to the file and line where the error occurred. You can grab the user tool configuration I use from here on the EditPlus wiki.
Perhaps there is similar functionality you can configure that allows you to run your program and display its output in your Emacs editor!?
I replaced cmd with Cygwin and Poderosa. May be a little overkill though, if the only problem you have with cmd is that it's a pain to resize.
Although you use Emacs instead of Vim, so I guess you're into overkill... ;-)

Categories