473,796 Members | 2,677 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

os.environ and os.path.chdir

Hi,
How to make changes to os.environ and os.path.chdir still effective after
I run the script? Currently, the changes are only good within my script. I
would like the shell who called python myprog.py also gets the change..
Thank you!

Yun
Jul 18 '05 #1
4 10188
Quoting Yun Mao (ma**@cis.upenn .edu):
Hi,
How to make changes to os.environ and os.path.chdir still effective after
I run the script? Currently, the changes are only good within my script. I
would like the shell who called python myprog.py also gets the change..
Thank you!


To the best of my knowledge, this can't be done. Depending on the
specific application, you might have several workarounds.

One is to exit into a new shell, using os.execve instead of sys.exit.
That creates "nested" shells, unless the user exec's your program
also.

Another is to exit by printing a list of commands, and expect that the
user will run your program like "eval `myprog.py`" to execute those
commands.

As far as I know, though, there's no way to actually hijack the user's
shell process. The user needs to explicitly give you that control.

--G.

--
Geoff Gerrietts "There is no fate that cannot be
<geoff at gerrietts net> surmounted by scorn." --Albert Camus

Jul 18 '05 #2
Yun Mao wrote:

How to make changes to os.environ and os.path.chdir still effective after
I run the script? Currently, the changes are only good within my script. I
would like the shell who called python myprog.py also gets the change..


Is there some Python programming course where all you guys are
coming from lately? :-) This question seems very popular this week.

As John Roth just replied to one of your potential classmates :-),
"Look at the thread "Setting Environment Variables" that started
yesterday in the early morning." ... and note that the comments
about environment variables apply equally to the current directory,
as both are properties of the calling shell and can be changed only
by scripts run by that shell.

-Peter
Jul 18 '05 #3
Geoff Gerrietts fed this fish to the penguins on Friday 19 September
2003 04:30 pm:


As far as I know, though, there's no way to actually hijack the user's
shell process. The user needs to explicitly give you that control.
A fully integrated REXX system could do it... This requires the
"shell" to process IBM's default "queue". The REXX program would write
the "CD ..." command to the QUEUE and, on exit, the command interpreter
would read/process whatever is in the queue before reading keyboard
input.

I don't believe the Linux OREXX is quite that integrated.

-- =============== =============== =============== =============== == <
wl*****@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
=============== =============== =============== =============== == <
Bestiaria Home Page: http://www.beastie.dm.net/ <
Home Page: http://www.dm.net/~wulfraed/ <


Jul 18 '05 #4
Geoff Gerrietts <ge***@gerriett s.net> wrote:
Quoting Yun Mao (ma**@cis.upenn .edu):
Hi,
How to make changes to os.environ and os.path.chdir still effective after
I run the script? Currently, the changes are only good within my script. I
would like the shell who called python myprog.py also gets the change..
Thank you!
To the best of my knowledge, this can't be done. [...]As far as I know, though, there's no way to actually hijack the user's
shell process. The user needs to explicitly give you that control.


Actually, there are a couple of ways to do it, at least under
Unix. However, they have their problems...

One way is to stuff characters into the terminals input buffer
using the TIOCSTI ioctl. Like this:

import fcntl, termios
for c in "cd /usr/lib\n":
fcntl.ioctl(1, termios.TIOCSTI , c)

It only works if it is being run interctively, under a tty,
though. If you have a shell script running the above program,
the shell script won't see the command, and thus won't change
directory. It will be the original shell that gets the command.
And if you're not on a tty, it doesn't work at all.

Also, the command you stuff into the terminal can get mixed up
with normal type-ahead typed by the user, leaving some garbled
command for the shell to read.

So, it's not very reliable.
Another way is to attach a debugger to the parent process, and
force it to do the chdir() system call, or poke around in its
memory to change the environment variables. Like this:

import os
gdb = os.popen('gdb $SHELL %d' % (os.getppid(),) , 'w')
gdb.write('call chdir("/usr/lib")\n')
gdb.write('deta ch\n')
gdb.write('quit \n')
gdb.close()

Untested code, but you probably get the idea.

Changing the environment variables of the parent is a bit
trickier. You need to know the memory layout of the parent,
so you can poke the correct bytes. You might be able to locate
the variable 'environ' and follow it, but there is no guarantee
it is still pointing to anything useful.
However, I would recommend *against* both of these methods.
Don't try any of this stuff youselves, kids; always get a parent
to supervise this kind of experiments! :-)
--
Thomas Bellman, Lysator Computer Club, Linköping University, Sweden
"The one who says it cannot be done should ! bellman @ lysator.liu.se
never interrupt the one who is doing it." ! Make Love -- Nicht Wahr!
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3841
by: Erick Bodine | last post by:
I am trying to set a new environment variable on a W2k machine with only partial success. The name("SSID") and value("ASIM") show up correctly in the registry and when I go to "System Properties"->Advanced->"Environment Variables". However, if I open a console and type 'set', "SSID" is not listed; also if I open a python shell and do os.environ the variable is not found. What am I doing wrong??? import _winreg
31
3930
by: John Roth | last post by:
I'm adding a thread for comments on Gerrit Holl's pre-pep, which can be found here: http://tinyurl.com/2578q Frankly, I like the idea. It's about time that all of the file and directory stuff in the os module got objectified properly (or at least with some semblance of OO propriety!) In the issues section:
0
1738
by: Tom McDavid | last post by:
I'm trying to learn more Python and have come across a strange behavior. I have Python2.3 installed on a Win2K machine. If I start the python command line, I can do the following: Python 2.3.2 (#49, Oct 2 2003, 20:02:00) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> os.getcwd() 'C:\\Python23'
2
3433
by: Tim Black | last post by:
In my recent experience, popen os pipes always fail when cwd is a UNC path. Can anyone shed any light on this? Although I've seen lots of UNC path-related problems in this newsgroup, I've not been able to find anything specifically about os pipes and UNC paths. Here's a session dump that demonstrates what I'm talking about: Python 2.3.4 (#53, May 25 2004, 21:17:02) on win32 Type "help", "copyright", "credits" or "license" for more...
11
3945
by: jim.eggleston | last post by:
Windows doesn't have a HOME environment variable, but it does have HOMEDRIVE and HOMEPATH. Could Windows versions of Python automatically populate os.environ with HOME, where HOME = os.path.join(os.environ, os.environ)? If this was done, then modules such as pdb, which load resource files from HOME, would work under Windows. Alternatively, here is a patch to make pdb.py read .pdbrc under Windows.
2
2905
by: eight02645999 | last post by:
hi i am writing a CGI to process some database transactions using the Sybase module. so in my CGI script, i have: .... import Sybase import cgitb; cgitb.enable(display=1 , logdir="/tmp/weblog.txt") .... ....
1
2389
by: peterbe | last post by:
This is a hard question to ask because I can't reproduce the problem other than restarting several times over until it happens again. I'm using Zope and Plone for a website on this debian linux vserver and when I restart it it runs some /etc/init.d/zope restart scripts and things fail in zope because somewhere deep in there the following raises an AttributeError that 'PATH' doesn't exist: foo = os.environ
9
3750
by: boris.smirnov | last post by:
Hi there, I have a problem with setting environment variable in my script that uses qt library. For this library I have to define a path to tell the script whre to find it. I have a script called "shrink_bs_070226" that looks like this: ********************************** import sys, re, glob, shutil import os
8
37246
by: Michael Robertson | last post by:
I'm having trouble opening a file in linux, whose path has spaces in it. $ mkdir my\ test $ echo test my\ test/test.txt $ python Exception Exception but yet...
0
9684
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10236
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10182
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10017
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6793
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5445
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.