Using Anaconda Python, esp. ipython, in cygwin, esp. emacs - python

I am working on a good Python environment for use on a Windows 7 machine. I am a big emacs fan, and like using the windows native build that's part of cygwin (emacs-w32). This way, I have access to all the cygwin tools from within emacs.
I also really like Anaconda's python distribution. I really don't feature downloading and maintaining all those packages myself, esp. within cygwin.
Also a big ipython fan.
On a Linux cluster, I'm having good luck using ipython as my python interpeter within emacs. I make a lot of plots, so matplotlib has to work too.
So I'm trying to get "ipython interpeter within cygwin emacs" to work.
Is this a good idea? Better ways to go? I know there's Spyder, but I honestly like working within emacs for editing .py files. Not just emacs keybindings - the whole emacs environment (grep, ediff, dired, etc etc).
Right now, I've gotten emacs to pick up ipython in the python 3 anaconda install. It runs, but I get extransous characters, e.g.:
^A^BIn [^A^B1^A^B]: ^A^B
instead of In [ 1 ]:
Also figure windows tend to not work (figure appears, but Windows says Not Responding, plots don't appear).
Thanks.
Update:
Thanks for the response. I did some more playing. To be exact:
* Setup bash path so Anaconda python and ipython found
* Started ConEmu (windows terminal program)
* ran bash from a cmd (dos) prompt
* ran cygwin's emacs: emacs -nw -Q (no window and no customization)
* Open .py file, C-c C-z, runs Anaconda python -i
* Get these messages:
Make dedicated process? (y or n) n
Making python-shell-interpreter local to Python while let-bound!
Making python-shell-interpreter-args local to Python while let-bound!
funcall: Args out of range: "^M", 0, 2
The final line appears in the emacs "status line" at the bottom. There is a buffer with Python running, seems to be fine. Except Ctrl-D doesn't work. If I type 'quit', I get the message "Use quit() or Ctrl-Z plus Return to exit." What happened to Ctrl-D?
If I repeat the exercise with ipython, the same messages are given, ipython buffer runs, I get the random ^A^B's, Ctrl-D doesn't work.

Related

What is the difference between running python in terminal and in IDE?

I'm new in Python. I'm using Windows 7.
When I install pip on my computer, I need to use the installing program: get-pip.py.
I found 3 ways to run this .py file:
In cmd, type "python get-pip.py"
Open it with IDLE and F5
Double click get-pip.py
I have two questions:
The only way can install it is "1. In cmd, type "python get-pip.py". I wonder what's the difference between 1 2 and 3?
Usually, when I double click .py file, it is usually opened by idle(default). But this time, when I double click get-pip.py, it begins running as if a .exe file. Why?
Thank you.
Answer to question 1.
If you run python from within a command window (cmd) you will be able to see the output if any. As opposed to double clicking it which will run the program and close it unless the program is supposed to do otherwise. And Idle is mimicking the open command window.
One thing to watch out for is having 2 python interpreters on the system. If you are just starting out I would avoid installing a 2nd one. Removing old python interpreters is as easy as removing the folder they are in. You said it "runs differently" which makes me think this is a concern.
Answer to question 2
You can change what program uses the file. You can have idle read it, or the python interpreter run it. check out http://www.thewindowsclub.com/change-file-associations-windows for info on how to change that behavior.

Problems with different versions of python

Questions similar to this one have been asked but I haven't seen a solution yet that helps me.
I am running Windows 7 and I've installed two versions of python, 3.3 and 2.7. Python 3.3 was the first version I installed and I was able to run scripts from the desktop (not the command line). I installed python 2.7 so that I could get numpy, scipy, and matplotlib down the road, but I found that all the scripts on my desktop defaulted to python 2.7. Since I coded in 3.3 this caused some issues.
I was able to fix this by right clicking the script icon, browsing programs, navigating to the python 3.3 file in my C: drive, and selecting the idle inside that directory. But then I found I wasn't able to run those scripts with python 2.7 using the exact same procedure.
Unfortunately I don't know command-line programming very much at all, and it seemed like most of the answers were geared towards that as a solution. Ideally I'd like to specify which python I want to run a script from the desktop, or possibly while I'm editing the script.
If it's relevant I'm running the eric python IDE, version 5.
I'd be happy to do the work of reading something fairly technical and long (like a blog post or PDF), but it won't help me much if it isn't aimed at beginners.
The Windows Python Launcher can automatically detect the Python from the shebang in your file.
Start your Python 3.3 scripts with
#!/usr/bin/env python3
and your Python 2.7 scripts with
#!/usr/bin/env python2.7
If you want to use the system's default Python version, you can just start the file with
#!/usr/bin/env python
This will also work on virtually all Unix systems.
Maybe you should chose another IDE, like PyCharm, it could manage different versions of python easily (and gives tons of other useful features). Perhaps free PyDev could also make it.
Also, using virtualenv — is the best solution here, but it's a "hard way".
The method I use is to have several cmd.exe shortcuts on my desktop, each pointing at a different runpython.bat file, one for each version of python. Here is one command example from a shortcut:
%comspec% /k "C:\QA\Python\QAPYTH3\runpython.bat"
Here is a typical runpython.bat:
#SET PATH=%PATH%;"C:\Python32"
#SET PYTHONPATH=C:\Python32\Lib
#ASSOC .py=Python.File
#ASSOC .pyc=Python.CompiledFile
#ASSOC .pyo=Python.CompiledFile
#ASSOC .pyw=Python.NoConFile
#FTYPE Python.CompiledFile="C:\Python32\python.exe" "%%1" %%*
#FTYPE Python.File="C:\Python32\python.exe" "%%1" %%*
#FTYPE Python.NoConFile="C:\Python32\pythonw.exe" "%%1" %%*
#SET PATHEXT=.py;%PATHEXT%
Of course you don't have to call your .bat files the same as I have. Just adjust to suite your setup.

How to install python 3.2.3 on Windows 7 enterprise

although I have been using python a long time very easily in a Linux environment, I have tremendous trouble to even install it correctly in a windows environment. I hope this is a question to be asked here, as it is not directly a programming question.
Especially, I have the following problems:
When on the command line, python is not a recognized command. Do I have to set the Windows path manually myself? If so, how to do that?
When starting a python script, should this be done with python.exe or pythonw.exe? What is the difference?
I also tried to install ipython several times, it never got installed (even after following the starting ipythonenter link description here thread.
When starting a script with python.exe, a window pops up and closes immediately. I saw some hints in putting in a readline command, which is of no help if there is a syntax error in the script. So how to be able to keep the window open, or how to run the command on the cmd.exe?
Thank you for any help on these items.
Alex
1) Look here: www.computerhope.com/issues/ch000549.htm
2) It has already been answered, always try to use search before asking question:
pythonw.exe or python.exe?
4) When using cmd.exe just navigate to your script folder using dir for changing directories and C:,D:,etc. for changing drives. Then run script by typing just the script name. When installed correctly, Python automatically launches .py scripts with python, so you don't have to write 'python' before script name. When run in cmd, window will stay open. If you want it to stay open even when launching script with double-click, use function waiting for user input, see here How to keep a Python script output window open?
You might want to use Python3.3, there is a new launcher for Python scripts in it. By that, you can start Python scripts with py <scriptname> which has the benefit of being installed in your path (C:\Windows\system32) and you can use a shebang to tell whether the script is for Python2 or Python3.
Also
In addition to the launcher, the Windows installer now includes an
option to add the newly installed Python to the system PATH
(contributed by Brian Curtin in issue 3561).

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.

Using Windows Python from Cygwin

I've been using Cygwin on Windows recently. I want to use the Windows installation of Python, so during testing I'm using /cygdrive/c/Python26/python.exe myfile.py rather than python myfile.exe.
This is working almost perfectly, except for printing. When I run the Windows Python from Cygwin the output doesn't print until execution finishes. It works fine running in Windows Python from explorer.exe or cmd.exe, and it works in Cygwin using the Cygwin-installed Python (/bin/python.exe).
Is there a workaround for this? The important thing is to be able to run the Windows version, but I'd like to do it all from with Bash.
The real problem is that when you run a command in any of the Cygwin terminal programs like mintty, they don't act as Windows Consoles. Only Windows Console-based ones like CMD or Console2 do that. So, with Cygwin terminals the Windows python.exe doesn't think it is talking to an interactive console.
That leads to buffering output instead of flushing buffers on every line as is done in interactive sessions. That is why Amro's adding the flush() on every line fixes the symptom, but means changing the code.
One solution without changing the code is to turn off buffering in Python using the '-u' flag on the command line or setting the PYTHONUNBUFFERED environment variable.
export PYTHONUNBUFFERED=1
/cydrive/c/Python27/python.exe foo.py
or
/cydrive/c/Python27/python.exe -u foo.py
or run in interactive mode
/cydrive/c/Python27/python.exe -i foo.py
You will also not be able to run the Windows python.exe interactive mode in the Cygwin terminal. It will not bring up an interactive session, but will just hang. I find the best solution seems to be to use 'cygstart' (better than using the '-i' option):
cygstart /cygdrive/c/Python27/python.exe
And that seems to work with ipython as well (if installed):
cygstart /cygdrive/c/Python27/Scripts/ipython.exe
Not answering the initial question, but for those who want to use Python interactive session from within Cygwin terminal (for example in mintty) - start Python with "-i" option to tell it explicitly that it needs to run in interactive mode:
$ python -i
The neat way is also to create an alias in your .bashrc (knowing that it is only read for interactive terminal sessions anyway):
alias python='python -i'
Otherwise, Python will not know that it runs in the console, because all Cygwin pty-based terminals (mintty, rxvt and xterm) are recognized as pipes by Windows, not as the console. Therefore, Python thinks there is no console and enters non-interactive mode. So, if you still want interactive mode instead, you need to explicitly tell Python to use it. However, it still won't behave as it normally should - one still won't be able to use HOME or LEFT ARROW keys, and so on.
Perhaps if you flush the output
import sys
V = range(100000)
for x in V:
print x
sys.stdout.flush()

Categories