Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subprocess module errors with 'export' in python on linux?

I'm setting up a program to connect my computer to our schools proxy and currently have something like this:

import subprocess
import sys

username = 'fergus.barker'
password = '*************'
proxy = 'proxy.det.nsw.edu.au:8080'
options = '%s:%s@%s' % (username, password, proxy)

subprocess.Popen('export http_proxy=' + options)

But upon running I get:

Traceback (most recent call last):
File "school_proxy_settings.py", line 19, in <module>
 subprocess.Popen('export http_proxy=' + options)
File "/usr/lib/python2.6/subprocess.py", line 621, in __init__
 errread, errwrite)
File "/usr/lib/python2.6/subprocess.py", line 1126, in _execute_child
 raise child_exception
OSError: [Errno 2] No such file or directory

Why is this happening please guys?

like image 312
Fergus Barker Avatar asked Oct 14 '10 01:10

Fergus Barker


People also ask

How do I run a Python export command?

From the menu in ModelBuilder, point to and click Model > Export > To Python Script. Click the Save in drop-down arrow and navigate to the location where you want to save your script. Type a file name for the script. Click Save.

How do you export in Python?

export is a command that you give directly to the shell (e.g. bash ), to tell it to add or modify one of its environment variables. You can't change your shell's environment from a child process (such as Python), it's just not possible. Here's what's happening when you try os. system('export MY_DATA="my_export"') ...

What is the subprocess module in Python?

Subprocess in Python is a module used to run new codes and applications by creating new processes. It lets you start new applications right from the Python program you are currently writing. So, if you want to run external programs from a git repository or codes from C or C++ programs, you can use subprocess in Python.

What does export Do Linux?

Export is a built-in command of the Bash shell. It is used to mark variables and functions to be passed to child processes. Basically, a variable will be included in child process environments without affecting other environments.


1 Answers

The problem is that export is not an actual command or file. It is a built-in command to shells like bash and sh, so when you attempt a subprocess.Popen you will get an exception because it can not find the export command. By default Popen does an os.execvp() to spawn a new process, which would not allow you to use shell intrinsics.

You can do something like this, though you have to change your call to Popen.

http://docs.python.org/library/subprocess.html

You can specify shell=True to make it use shell commands.

class subprocess.Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)

On Unix, with shell=True: If args is a string, it specifies the command string to execute through the shell. This means that the string must be formatted exactly as it would be when typed at the shell prompt. This includes, for example, quoting or backslash escaping filenames with spaces in them. If args is a sequence, the first item specifies the command string, and any additional items will be treated as additional arguments to the shell itself. That is to say, Popen does the equivalent of:

Popen(['/bin/sh', '-c', args[0], args[1], ...])

like image 191
逆さま Avatar answered Oct 24 '22 09:10

逆さま