Home Download Buy Blog Forum Support

Embedded Python version & subprocess

Embedded Python version & subprocess

Postby deek on Mon Feb 04, 2013 11:26 pm

I'm new to Python in general so forgive me if this is a simple question that I was unable to find an answer for by searching.

I am writing a plugin that will make some various system calls and then act on the results. Reading the Python docs it suggested to use subprocess for these commands. I have been having issues getting it to work and after some research I found it is because the version of Python that sublime plugins use is 2.6 and the subprocess module has some features in 2.7. Is there a method for using a different Python version or should I not even be using the subprocess call?
deek
 
Posts: 4
Joined: Mon Feb 04, 2013 11:19 pm

Re: Embedded Python version & subprocess

Postby sapphirehamster on Tue Feb 05, 2013 4:09 pm

On OSX, ST2 will use your system Python. On other platforms, it ships with Python 2.6. In ST3, it is now Python 3 on all platforms.

What problems are you having with the subprocess module?
sapphirehamster
 
Posts: 83
Joined: Sun Jul 01, 2012 11:19 pm

Re: Embedded Python version & subprocess

Postby deek on Tue Feb 05, 2013 6:04 pm

When I do a
Code: Select all
print(subprocess.__file__)

the return is:
Code: Select all
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py
which is not the same version of Python my system is using.
Code: Select all
$ python -V
Python 2.7.2


The problem I am having with subproccess is I am not able to get the output back from the call so I can perform logic on it. The command i have trying is along the lines of
Code: Select all
args = self.server_name + ' "remote_cmd"'
subprocess.call(['ssh', args])


running the command on my terminal works fine but even doing something as simple as
Code: Select all
subprocess.call(["ls", "-l"], shell=True)
doesn't give me any result back.
deek
 
Posts: 4
Joined: Mon Feb 04, 2013 11:19 pm

Re: Embedded Python version & subprocess

Postby sapphirehamster on Tue Feb 05, 2013 7:18 pm

Sorry, I wasn't clear. On OS X, it always uses the 2.6 version of the system python, even if you have a newer version (you can see which versions of Python you have in /System/Library/Frameworks/Python.framework/Versions/ and "Current" is probably a symlink to 2.7). It's just a limitation of Sublime.

The "call" method just runs the command, it does not provide any output. You will need to use the Popen object to retrieve the output from the command. For example:

Code: Select all
output = subprocess.Popen(['ls', '-l'], stdout=subprocess.PIPE).communicate()[0]


You can check out the older Python docs here: http://docs.python.org/release/2.6.8/li ... ocess.html
sapphirehamster
 
Posts: 83
Joined: Sun Jul 01, 2012 11:19 pm

Re: Embedded Python version & subprocess

Postby deek on Tue Feb 05, 2013 8:10 pm

That worked for the simple case of ls -l. I tried the same setup with the ssh command I am running and I'm not getting any errors but no output. Is there anything else that needs to be taken into account when running commands over ssh?

The command below works fine in the terminal but I get nothing back in from the plug-in/sublime.

Code: Select all
args = self.remote_sever_name + ' "<remote command> ' + self.remote_server_root + self.repo_name + self.git_file + '"'
output = subprocess.Popen(['ssh', args], stdout=subprocess.PIPE).communicate()[0]

Thank you for your help!
deek
 
Posts: 4
Joined: Mon Feb 04, 2013 11:19 pm

Re: Embedded Python version & subprocess

Postby sapphirehamster on Wed Feb 06, 2013 5:08 am

You can help debug your situation by including stderr=subprocess.STDOUT in your Popen arguments. This will let you see the error message.

I think you need to make sure that args is a list. AFAIK, the Popen module will not split the command line for you. Something like:
Code: Select all
args = ['user@example.com', 'ls', '-l']
subprocess.Popen(['ssh']+args, ...)
sapphirehamster
 
Posts: 83
Joined: Sun Jul 01, 2012 11:19 pm

Re: Embedded Python version & subprocess

Postby deek on Wed Feb 06, 2013 7:53 pm

I got it! Turns out two things were happening. The first was that every time you would use a space it would be a new value in the list. The second was that certain responses of the program I was running on the remote server were putting output to stderr not stdout like I thought.

Thank you so much for your help sapphirehamster.
deek
 
Posts: 4
Joined: Mon Feb 04, 2013 11:19 pm


Return to Plugin Development

Who is online

Users browsing this forum: Majestic-12 [Bot] and 6 guests