Sublime Forum

Embedded Python version & subprocess

#1

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?

0 Likes

#2

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?

0 Likes

#3

When I do a

 print(subprocess.__file__) 

the return is:

/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.

$ 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

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 subprocess.call("ls", "-l"], shell=True) doesn’t give me any result back.

0 Likes

#4

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:

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

You can check out the older Python docs here: docs.python.org/release/2.6.8/li … ocess.html

0 Likes

#5

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.

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!

0 Likes

#6

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:

args = 'user@example.com', 'ls', '-l']
subprocess.Popen('ssh']+args, ...)
0 Likes

#7

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.

0 Likes