Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
01094e40
Commit
01094e40
authored
Feb 17, 1997
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Change the question about os.environ changes not working -- it now
works unless you don't have putenv.
parent
8651d27e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
10 additions
and
19 deletions
+10
-19
Misc/FAQ
Misc/FAQ
+10
-19
No files found.
Misc/FAQ
View file @
01094e40
...
...
@@ -1295,25 +1295,16 @@ to sys.stderr when this happens.
4.18. Q. How do I change the shell environment for programs called
using os.popen() or os.system()? Changing os.environ doesn't work.
A. Modifying the environment passed to subshells was left out of the
interpreter because there seemed to be no well-established portable
way to do it (in particular, some systems, have putenv(), others have
setenv(), and some have none at all).
However if all you want is to pass environment variables to the
commands run by os.system() or os.popen(), there's a simple solution:
prefix the command string with a couple of variable assignments and
export statements. The following would be universal for popen:
import os
from commands import mkarg # nifty routine to add shell quoting
def epopen(cmd, mode, env = {}):
# env is a dictionary of environment variables
prefix = ''
for key, value in env.items():
prefix = prefix + '%s=%s\n' % (key, mkarg(value)[1:])
prefix = prefix + 'export %s\n' % key
return os.popen(prefix + cmd, mode)
A. You must be using either a version of python before 1.4, or on a
(rare) system that doesn't have the putenv() library function.
Before Python 1.4, modifying the environment passed to subshells was
left out of the interpreter because there seemed to be no
well-established portable way to do it (in particular, some systems,
have putenv(), others have setenv(), and some have none at all). As
of Python 1.4, almost all Unix systems *do* have putenv(), and so does
the Win32 API, and thus the os module was modified so that changes to
os.environ are trapped and the corresponding putenv() call is made.
4.19. Q. What is a class?
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment