Permanently set Python path for Anaconda within Cygwin - python

I'm trying to install Anaconda on my Windows 7 machine. I often use cygwin to for my command-line work, and I would like to manage Anaconda from there. I've worked through the graphic installer without any issues, and checked necessary boxes to reset my default path to this install of python. I go ahead to check where python is and initially I get this...
$ which python
/usr/bin/python
From here python works fine...
$ python
Python 2.7.5 (default, Oct 2 2013, 22:34:09)
[GCC 4.8.1] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
But I'm trying to work from anaconda, so I should just need to redefine my path...
$ export PATH=/cygdrive/c/anaconda:$PATH
$ which python
/cygdrive/c/anaconda/python
And now I should be good to go, but when I try and step into python, it just hangs
$ python
Any idea why this might be happening? verbose return, below...
$ python -v
# installing zipimport hook
import zipimport # builtin
# installed zipimport hook
# C:\anaconda\lib\site.pyc matches C:\anaconda\lib\site.py
import site # precompiled from C:\anaconda\lib\site.pyc
# C:\anaconda\lib\os.pyc matches C:\anaconda\lib\os.py
import os # precompiled from C:\anaconda\lib\os.pyc
import errno # builtin
import nt # builtin
# C:\anaconda\lib\ntpath.pyc matches C:\anaconda\lib\ntpath.py
import ntpath # precompiled from C:\anaconda\lib\ntpath.pyc
# C:\anaconda\lib\stat.pyc matches C:\anaconda\lib\stat.py
import stat # precompiled from C:\anaconda\lib\stat.pyc
# C:\anaconda\lib\genericpath.pyc matches C:\anaconda\lib\genericpath.py
import genericpath # precompiled from C:\anaconda\lib\genericpath.pyc
# C:\anaconda\lib\warnings.pyc matches C:\anaconda\lib\warnings.py
import warnings # precompiled from C:\anaconda\lib\warnings.pyc
# C:\anaconda\lib\linecache.pyc matches C:\anaconda\lib\linecache.py
import linecache # precompiled from C:\anaconda\lib\linecache.pyc
# C:\anaconda\lib\types.pyc matches C:\anaconda\lib\types.py
import types # precompiled from C:\anaconda\lib\types.pyc
# C:\anaconda\lib\UserDict.pyc matches C:\anaconda\lib\UserDict.py
import UserDict # precompiled from C:\anaconda\lib\UserDict.pyc
# C:\anaconda\lib\_abcoll.pyc matches C:\anaconda\lib\_abcoll.py
import _abcoll # precompiled from C:\anaconda\lib\_abcoll.pyc
# C:\anaconda\lib\abc.pyc matches C:\anaconda\lib\abc.py
import abc # precompiled from C:\anaconda\lib\abc.pyc
# C:\anaconda\lib\_weakrefset.pyc matches C:\anaconda\lib\_weakrefset.py
import _weakrefset # precompiled from C:\anaconda\lib\_weakrefset.pyc
import _weakref # builtin
# C:\anaconda\lib\copy_reg.pyc matches C:\anaconda\lib\copy_reg.py
import copy_reg # precompiled from C:\anaconda\lib\copy_reg.pyc
# C:\anaconda\lib\traceback.pyc matches C:\anaconda\lib\traceback.py
import traceback # precompiled from C:\anaconda\lib\traceback.pyc
# C:\anaconda\lib\sysconfig.pyc matches C:\anaconda\lib\sysconfig.py
import sysconfig # precompiled from C:\anaconda\lib\sysconfig.pyc
# C:\anaconda\lib\re.pyc matches C:\anaconda\lib\re.py
import re # precompiled from C:\anaconda\lib\re.pyc
# C:\anaconda\lib\sre_compile.pyc matches C:\anaconda\lib\sre_compile.py
import sre_compile # precompiled from C:\anaconda\lib\sre_compile.pyc
import _sre # builtin
# C:\anaconda\lib\sre_parse.pyc matches C:\anaconda\lib\sre_parse.py
import sre_parse # precompiled from C:\anaconda\lib\sre_parse.pyc
# C:\anaconda\lib\sre_constants.pyc matches C:\anaconda\lib\sre_constants.py
import sre_constants # precompiled from C:\anaconda\lib\sre_constants.pyc
# C:\anaconda\lib\locale.pyc matches C:\anaconda\lib\locale.py
import locale # precompiled from C:\anaconda\lib\locale.pyc
import encodings # directory C:\anaconda\lib\encodings
# C:\anaconda\lib\encodings\__init__.pyc matches C:\anaconda\lib\encodings\__init__.py
import encodings # precompiled from C:\anaconda\lib\encodings\__init__.pyc
# C:\anaconda\lib\codecs.pyc matches C:\anaconda\lib\codecs.py
import codecs # precompiled from C:\anaconda\lib\codecs.pyc
import _codecs # builtin
# C:\anaconda\lib\encodings\aliases.pyc matches C:\anaconda\lib\encodings\aliases.py
import encodings.aliases # precompiled from C:\anaconda\lib\encodings\aliases.pyc
import operator # builtin
# C:\anaconda\lib\functools.pyc matches C:\anaconda\lib\functools.py
import functools # precompiled from C:\anaconda\lib\functools.pyc
import _functools # builtin
import _locale # builtin
# C:\anaconda\lib\encodings\cp1252.pyc matches C:\anaconda\lib\encodings\cp1252.py
import encodings.cp1252 # precompiled from C:\anaconda\lib\encodings\cp1252.pyc
# zipimport: found 13 names in C:\anaconda\lib\site-packages\runipy-0.1.0-py2.7.egg
# zipimport: found 144 names in C:\anaconda\lib\site-packages\setuptools-3.6-py2.7.egg
Python 2.7.7 |Anaconda 2.0.1 (64-bit)| (default, Jun 11 2014, 10:40:02) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and htt
Another (possibly related) issue I'm having is needing to reset the path every time I close/open cygwin. I've entered the following text into .bashrc and .profile to try and set the path permanently:
# Set path to python from anaconda install
export PATH=/cygdrive/c/anaconda:$PATH
After opening and closing cygwin, I return to:
$ which python
/usr/bin/python
Could this be related to setting certain system environment variables?

To work with the interactive Python shell in Cygwin I use the -i option.
To get it from the Anaconda install, I used the steps suggested above:
$ export PATH=/cygdrive/c/anaconda:$PATH
$ which python
/cygdrive/c/anaconda/python
Then I launch python within Cygwin with the -i option:
$ python -i
Python 2.7.8 |Anaconda 2.1.0 (64-bit)| (default, Jul 2 2014, 15:12:11) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>>>
The libraries are also working fine. For instance the pandas library (which has been installed through Anaconda) can be imported now.
>>>> import pandas
>>>> pandas.DataFrame
<class 'pandas.core.frame.DataFrame'>
Now to make this permanent I added the path in my bashrc file:
$ echo 'export PATH=/cygdrive/c/anaconda:$PATH' >> .bashrc
$ source .bashrc
$ which python
/cygdrive/c/anaconda/python
I hope this helps.

I too was having an issue getting anaconda set up with my Cygwin / Windows 7 system. What worked was the following:
Edited the ~/.bashrc. Add below to the bottom of file.
export PATH=/cygdrive/c/Anaconda:$PATH
This mapped Cygwin's python to the anaconda distribution as proof by entering which python in Cygwin's console. However, when launching python it would hang up forcing me to ^C out of the command. I found that python -i will launch the interactive python interpreter with no issues.

Path
Regarding your path issue, see Installing anaconda to use with windows on how to fix that. Alternatively, you can just run /cygdrive/c/Anaconda/python.exe to start the Anaconda/windows python and leave the standard python pointing to the cygwin version.
Note that few of the suggestions given on the internet also include the necessary change to PYTHONPATH: if you use the anaconda/windows python, you also want the anaconda python libraries. Binary modules like numpy will surely crash or misbehave if you mix things up. The easiest way to check this is to install a module in cygwin that you do not need in Conda and test whether you can import that from anaconda python. If you can, your path is wrong.
Anaconda python will automatically add c:\Anconda to your PYTHONPATH, but if you have it set to serve CYGWIN python libraries, those will be found before the Anaconda libs. Try
import sys
sys.path
Terminal
However, this is also a terminal issue: Anaconda (Windows) python expects a windows shell and a modern cygwin shell is like an xterm, so very different. Diagnose this by running python --help, if that works but plain python hangs, the interactive prompt is the problem. This means you can run python programs, you just cannot interact with them.
A way around this is to use the cygwin bash shell instead of the cygwin terminal (both are under Cygwin group in the Start menu). They look very similar, but the font in the bash shell is less pretty and you cannot make the window wider than 80 chars (like all other CMD windows, never understood that 'feature'). This is cygwin bash running inside a CMD window, and your anaconda python will be interactive. The cygwin terminal gives you a bash shell running in a putty derivative, which mimics an xterm and cannot be used by anaconda python.

There is another problem with conda's interpretation of cygwin. When you source the activation script for an environment, it prepends e.g.
/C/Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah
/C/Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah/Library/mingw-w64/bin
/C/Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah/Library/usr/bin
/C/Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah/Library/bin
/C/Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah/Scripts
to your PATH. However, these are not proper directories in cygwin.
I fixed this with
ln -s /cygdrive/c /C
and then if you create conda environments with specific versions of python they should work.

Regarding setting your path permanently, edit the .profile in your home directory (it's a hidden file).
.profile (other names are also valid, see the bash man page) contains bash commands. It is executed when bash is started as login shell.
At the bottom of this file, put your set path command:
export PATH=/cygdrive/c/anaconda:$PATH
As for why python is hanging, please provide more information - for example what's the output of python -v?

I'm a little late in coming to this, but I was having the same problem as you #thomasshouler. Try adding the export line below to the end of the .bash_profile and .profile files as well as your .bashrc file (as mentioned above). Make sure to "source .bash_profile" etc for each file.
export PATH=/cygdrive/c/anaconda:$PATH

Once the Cygwin path is updated as mentioned above, you can also add an alias in the .bashrc file:
alias python='python -i'
This will bring up the Anaconda python interpreter in interactive mode from a Cygwin bash prompt

Found that for Anaconda 4, do:
vim /home/[Username]/.bash_profile
Append to the last line:
PATH=$PATH:/cygdrive/c/Users/[Username]/Anaconda3/Scripts/
This solution worked for me as the usual anaconda3/bin could not be found.

Related

Installed Libraries are not being found by my IDE (Mac) [duplicate]

I am trying to import pyodbc module on a windows computer. It works in the terminal, but not the IDLE. The error message in IDLE is:
Traceback (most recent call last):
File "FilePath/Filename.py", line 3, in <module>
import pyodbc
ImportError: No module named pyodbc
This typically occurs when multiple versions of python are installed with different paths. You can check to see if you have multiple installations by opening up the IDLE terminal and using
import sys
sys.version
sys.path
These commands will print the system PATH and version of the current instance of python. Use this in both IDLE and the command line terminal to see where each differ. Once you know which version is the one you want then just remove the other. You could also remove all python instances and then reinstall a clean python environment but then you would have to re-install all of your modules using pip or easy_install
Open python in cmd (type python and press enter)
Import the module in cmd (type import modulename)
Type modulename.__file__
You will get the path where the module is stored
Copy the corresponding folder
In IDLE, import sys and typing sys.executable to get the paths where it looks for modules to import
Paste your module's folder in the path where IDLE looks for modules.
This method worked for me.
You can pip show after install package and know about location where package installed.
After that check in IDLE sys.path and if directory with package not in sys.path try to add it.
import sys
sys.path.append("/home/dm/.local/lib/python3.6/site-packages")
# or another folder that `pip show` about package.
this happen because of multiple python installed (32bit version, 64bit version) or 3v and 2.7v so to solve this problem you have to invoke the idle for that specific version like this
cd to the dir of the version that the import work fine in cmd in that folder type this command below
pythonw.exe Lib\idlelib\idle.pyw
this command will invoke idle for that version
and the import will work fine
Me too had the same issue while trying to import a module which was successfully imported on terminal and not able to install on IDLE.
How I fixed?
Assuming you know how to execute commands on terminal as well as inside of python interpreter
Open your Terminal & execute the below commands :
:~$ python3
Python 3.6.9 (default, Jan 26 2021, 15:33:00)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
>>> import sys
>>> sys.version
'3.6.9 (default, Jan 26 2021, 15:33:00) \n[GCC 8.4.0]'
>>> sys.path
['', '/usr/lib/python36.zip', '/usr/lib/python3.6',
'/usr/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/dist-
packages', '/usr/lib/python3/dist-packages']
>>>
Now import your module inside of your python3 interpreter.
>>> import nester
>>>
>>> nester.__file__
'/usr/local/lib/python3.6/dist-packages/nester.py'
>>>
Open your IDLE and run the below commands and compare them
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927
64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more
information.
>>> import sys
>>> sys.version
'3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit
(AMD64)]'
>>> sys.path
['','C:\Users\username\AppData\Local\Programs\Python\Python39\Lib\idlelib', 'C:\Users\username\AppData\Local\Programs\Python\Python39\python39.zip', 'C:\Users\username\AppData\Local\Programs\Python\Python39\DLLs', 'C:\Users\username\AppData\Local\Programs\Python\Python39\lib', 'C:\Users\username\AppData\Local\Programs\Python\Python39', 'C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages']
>>> sys.executable
'C:\Users\username\AppData\Local\Programs\Python\Python39\pythonw.exe'
Now if you compare both outputs from Terminal & IDLE,
Terminal Module location is different from IDLE
I was using Ubuntu 18 terminal on windows machine
So I just copied my file to 'C' directory and ensured its file privileges. That's it.
:~$ cp -p /usr/local/lib/python3.6/dist-packages/nester.py /mnt/c/Users/username/AppData/Local/Programs/Python/Python39/Lib/
It worked!!
I Found the solution. It works for me
The problem is your installation directory does not match with the python version directory.
solution is >>>
type %localappdata% in your search bar then go to this folder.
here select the program folder. then select Programs , Python , Python version , Scripts
copy the location of the Scripts folder
open command prompt and type cd //yourpath (in my case cd C:\Users\3C HOUSE\AppData\Local\Programs\Python\Python37\Scripts)
if you wanna install numpy , now run pip install numpy
When you put your python scripts that have import pandas in the same folder as the site packages like pandas for example and use the same version of python that is used on CMD, it should help run your scripts in IDLE.
Check the path of your code, and that of the module. Copying the module to the path where code is worked for me.
'sys.executable' will give the path where code is stored.
For windows, open command prompt and enter pip show pyodbc to get the path of package and copy the path.
then open idle and run these lines
import sys
sys.path
Match the path from command prompt and the paths mentioned in the list provided by running above lines in IDLE. If the path is not mentioned then run these lines in idle
sys.path.append("Enter the copied path of package here")
After executing these lines, check again by importing the package that if it works for you.

/usr/local/bin/python3: bad interpreter: No such file or directory for ubuntu 14.04

Hi My python installation is in different directory and i am using a docker image which is mac based and it is referring shebang line as /user/local/bin/python3 from other folder in shell script .
my python installation path
Python 3.4.3 (default, Oct 14 2015, 20:28:29)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/home/myuser/project', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-x86_64-linux-gnu', '/usr/lib/python3.4/lib-dynload', '/usr/local/lib/python3.4/dist-packages', '/usr/lib/python3/dist-packages']
>>>
so is there a way without changing the shebang line i can redirect or link to my installation of python3 to get out of this error.
is it recommended to install python3 in given path. ?
please advice.
If you can't modify the shebang of the file, and you have access to the Dockerfile that creates your docker image, you can add a command directive to create a symbolic link: ln -s /usr/bin/python3 /usr/local/bin/.
If you don't have access to the Dockerfile. Then you can run the above command from within the running docker instance. That should solve your issue without having to modify the file.
https://docs.docker.com/engine/reference/builder/#cmd
You could set you shebang to "/usr/bin/env python" as usual, then set your path appropriately so that the correct version of python is on the path for your executable. In bash you can set the path on the command line using:
PATH=python/path:$PATH app
I will sometimes ignore the shebang and type python/path/python $(which app) in order to control which python interpreter is running.

Shebang for scripts not working

I am using PythonAnyhwere for my django production environment. I have a script which should run on scheduled basis.
Since I installed django in a virtualenv, the script starts like
#!/usr/bin/env python
activate_this = '/home/myname/.virtualenvs/myenv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
The error I get for this is
/usr/bin/env python: no such Python interpreter
No problem. So I changed it to
#!/usr/bin/env python2.7
Then I got
/usr/bin/env python2.7: no such Python interpreter
or
/usr/bin/env python3.4: no such Python interpreter
I said ok, what if I don't have a shebang line at all?
Error in log:
line 1: activate_this: command not found
line 2: syntax error near unexpected token `activate_this,'
line 2: `execfile(activate_this, dict(__file__=activate_this))'
What is the way then?
You can know where your Python interpreter by typing
$ which python
Also you try something like this (or maybe without env):
$ env python
Python 3.5.0 (default, Sep 20 2015, 11:28:25)
[GCC 5.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.path
<module 'posixpath' from '/usr/lib/python3.5/posixpath.py'>
And then change lib to bin and omit /posixpath.py part

Can't import the cx_Oracle module unless I'm using an interactive shell

When using Python on an interactive shell I'm able to import the cx_Oracle file with no problem. Ex:
me#server~/ $ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>>
As you can see, importing works without a hitch. However, when I try to run a Python script doing the same thing, I get an error:
me#server~/ $ sudo script.py
Traceback (most recent call last):
File "/usr/local/bin/script.py", line 19, in <module>
import cx_Oracle
ImportError: No module named "cx_Oracle'
Here is the important section from script.py:
# 16 other lines above here
# Imports
import sys
import cx_Oracle
import psycopg2
...
I'm befuddled here. Other pertinent information is the server I'm running is Ubuntu 14.04.1 LTS (upgraded from 12.04) 64bit. which python and sudo which python both point to the same location. Also, doing this as root via sudo su - gets the same results; import OK from interactive but error from script.
Nothing other than the OS upgrade happened between when this worked and when it stopped working.
Sorry, all. This was a silly on my part. Turns out the script in question was using Python3, and when the server upgraded, Python3 went from being 3.2 version to being 3.4 version.
Once the cx_Oracle module was set up in the 3.4 version, everything worked as expected.
Phil, your final note talking about the shebang was what lead me to discover this, so kudos to you! The reason I didn't mark your response as the answer was because technically it wasn't but led me on the right path.
Cheers!
sudo starts a new bash environment which is then pointing to a different python executable (different installed modules).
You can verify this with which python and sudo which python
EDIT: so if they point to the same executable, then you should look at sys.path to find differences. In both environemnts you can:
python -c "import sys; print('\n'.join(sys.path))"
sudo python -c "import sys; print('\n'.join(sys.path))"
Look for differences. If there are none:
A common error in import situations like this is that python will first look at the local dir. So if you happen to be running python and importing something what is found locally (i.e. cx_Oracle is a subdir of your current location), you will get an import error if you change directories.
Final note: I have assumed here that the shbang of the script.py points to the same executable as which python. That is, that python script.py and script.py return the same error.

How to launch an EDITOR (e. g. vim) from a python script?

I want to call up an editor in a python script to solicit input from the user, much like crontab e or git commit does.
Here's a snippet from what I have running so far. (In the future, I might use $EDITOR instead of vim so that folks can customize to their liking.)
tmp_file = '/tmp/up.'+''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(6))
edit_call = [ "vim",tmp_file]
edit = subprocess.Popen(edit_call,stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True )
My problem is that by using Popen, it seems to keep my i/o with the python script from going into the running copy of vim, and I can't find a way to just pass the i/o through to vim. I get the following error.
Vim: Warning: Output is not to a terminal
Vim: Warning: Input is not from a terminal
What's the best way to call a CLI program from python, hand control over to it, and then pass it back once you're finished with it?
Calling up $EDITOR is easy. I've written this kind of code to call up editor:
import sys, tempfile, os
from subprocess import call
EDITOR = os.environ.get('EDITOR', 'vim') # that easy!
initial_message = '' # if you want to set up the file somehow
with tempfile.NamedTemporaryFile(suffix=".tmp") as tf:
tf.write(initial_message)
tf.flush()
call([EDITOR, tf.name])
# do the parsing with `tf` using regular File operations.
# for instance:
tf.seek(0)
edited_message = tf.read()
The good thing here is the libraries handle creating and removing the temporary file.
In python3: 'str' does not support the buffer interface
$ python3 editor.py
Traceback (most recent call last):
File "editor.py", line 9, in <module>
tf.write(initial_message)
File "/usr/lib/python3.4/tempfile.py", line 399, in func_wrapper
return func(*args, **kwargs)
TypeError: 'str' does not support the buffer interface
For python3, use initial_message = b"" to declare the buffered string.
Then use edited_message.decode("utf-8") to decode the buffer into a string.
import sys, tempfile, os
from subprocess import call
EDITOR = os.environ.get('EDITOR','vim') #that easy!
initial_message = b"" # if you want to set up the file somehow
with tempfile.NamedTemporaryFile(suffix=".tmp") as tf:
tf.write(initial_message)
tf.flush()
call([EDITOR, tf.name])
# do the parsing with `tf` using regular File operations.
# for instance:
tf.seek(0)
edited_message = tf.read()
print (edited_message.decode("utf-8"))
Result:
$ python3 editor.py
look a string
Package python-editor:
$ pip install python-editor
$ python
>>> import editor
>>> result = editor.edit(contents="text to put in editor\n")
More details here: https://github.com/fmoo/python-editor
click is a great library for command line processing and it has some utilities, click.edit() is portable and uses the EDITOR environment variable. I typed the line, stuff, into the editor. Notice it is returned as a string. Nice.
(venv) /tmp/editor $ export EDITOR='=mvim -f'
(venv) /tmp/editor $ python
>>> import click
>>> click.edit()
'stuff\n'
Check out the docs https://click.palletsprojects.com/en/7.x/utils/#launching-editors My entire experience:
/tmp $ mkdir editor
/tmp $ cd editor
/tmp/editor $ python3 -m venv venv
/tmp/editor $ source venv/bin/activate
(venv) /tmp/editor $ pip install click
Collecting click
Using cached https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl
Installing collected packages: click
Successfully installed click-7.0
You are using pip version 19.0.3, however version 19.3.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(venv) /tmp/editor $ export EDITOR='=mvim -f'
(venv) /tmp/editor $ python
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import click
>>> click.edit()
'stuff\n'
>>>
The PIPE is the problem. VIM is an application that depends on the fact that the stdin/stdout channels are terminals and not files or pipes. Removing the stdin/stdout paramters worked for me.
I would avoid using os.system as it should be replaced by the subprocess module.
The accepted answer does not work for me. edited_message stays the same as initial_message. As explained in the comments, this is caused by vim saving strategy.
There are possible workarounds, but they are not portable to other editors. Instead, I strongly recommend to use click.edit function. With it, your code will look like this:
import click
initial_message = "edit me!"
edited_message = click.edit(initial_message)
print(edited_message)
Click is a third-party library, but you probably should use it anyway if you are writing a console script. click to argparse is the same as requests to urllib.

Categories