Another question from a not even newbie:
In Unix you can set an environment variable with the command
export PYTHONPATH
but I would like to set the variable from at .py script.
So my question is:
How do I export an environment variable in a .py script?
Thanks
Chris 28 20978
Just use
os.system("expo rt PYTHONPATH = %s" %("your_pythonp ath"))
In Unix, you generally can't affect the environment of your parent program (in
a broad sense, which includes environment variables, current working directory,
opened files, effective user ID, etc).
You have two basic choices to achieve an effect like this. First, you can
start a subshell with the desired environment, something like (untested)
os.environ['VARIABLE'] = 'value'
os.system(os.en viron['shell'])
(commands like 'su' work in this way)
Second, you can produce shell commands that have the effect of changing a
shell's environment when they are executed. Something like:
print "VARIABLE=value "
To use this, the user must write something like
eval `myscript.py`
instead of just
myscript.py
this can typically be encapsulated in a shell alias or function.
(commands like 'resize' (which sets the shell's idea of a terminal's size) work
this way)
Finally, you might be able to use an OS-specific interface like linux' ptrace
to change the actual contents of a calling process's memory. I didn't
immediately find an example of this, but on Linux it would consist of using
PTRACE_PEEKDATA and PTRACE_POKEDATA to locate the environment in another
process and modify it. The ptrace interface is not available from any standard
Python module, and isn't portable anyway. (though my manpage actually says
'CONFORMING TO SVr4, ..., X/OPEN, BSD 4.3' so maybe ptrace is more portable
than I believed)
Jeff
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
iD8DBQFDV4sxJd0 1MZaTXX0RAj4aAJ 41BlvbrZxLNrhm+ Ea5bgMyZOkVwQCg lsLw
wFB6iqRqw3FRzno zCpV5qh0=
=MqJa
-----END PGP SIGNATURE-----
Thanks Jeff and the crazy 88.
On 20 Oct 2005 01:58:44 -0700, the_crazy88 <py****@gentool inux.demon.nl> wrote: Just use os.system("expo rt PYTHONPATH = %s" %("your_pythonp ath"))
.... except it won't work: os.system will execute the command in a new process, so the environment variable change will only be visible in *this* process. Since you don't do anything else, the environment variable change will never be seen by anyone.
As for the OP's question, the short answer is "you can't": the Python interpreter will always be executed in a different process than the calling shell, and environment variable changes *never* impact the calling process on Unix.
The closest thing you can do is that:
-myScript.py--------------------------------------
print 'export MY_VARIABLE=val ue'
--------------------------------------------------
-myScript.sh--------------------------------------
python myScript.py > /tmp/chgvars.sh
.. /tmp/chgvars.sh
--------------------------------------------------
This is quite ugly: you write the shell commands changing the environment variables to a file, then "source" this file in the calling shell. But this is probably the best way to do what you want.
HTH
--
python -c "print ''.join([chr(154 - ord(c)) for c in 'U(17zX(%,5.zmz 5(17;8(%,5.Z65\ '*9--56l7+-'])"
On 2005-10-20, the_crazy88 <py****@gentool inux.demon.nl> wrote: os.system("expo rt PYTHONPATH = %s" %("your_pythonp ath"))
No, that won't work.
That will set the environment variable in the shell spawned by
the os.system command. That shell will then immediately exit,
leaving the caller's environment unchanged.
--
Grant Edwards grante Yow! Are you mentally here
at at Pizza Hut??
visi.com
On 2005-10-20, Christian <ch*******@spam .not> wrote: How do I export an environment variable in a .py script? http://www.python.org/doc/current/li....html#l2h-1548
--
Grant Edwards grante Yow! My BIOLOGICAL ALARM
at CLOCK just went off... It
visi.com has noiseless DOZE FUNCTION
and full kitchen!!
"Eric Brunel" <er*********@de spammed.com> writes: -myScript.py-------------------------------------- print 'export MY_VARIABLE=val ue' --------------------------------------------------
-myScript.sh-------------------------------------- python myScript.py > /tmp/chgvars.sh . /tmp/chgvars.sh
It's simpler to use eval and command substitution:
eval $(python myScript.py)
<mike
--
Mike Meyer <mw*@mired.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
> The closest thing you can do is that:
-myScript.py-------------------------------------- print 'export MY_VARIABLE=val ue' --------------------------------------------------
-myScript.sh-------------------------------------- python myScript.py > /tmp/chgvars.sh . /tmp/chgvars.sh --------------------------------------------------
Can I write a .py script that calls a .sh script that executes the
export command and then calls another .py script (and how would the
first .py script look)?
That would be much more what is my basic problem.
Thanks
Chris
Christian wrote: Can I write a .py script that calls a .sh script that executes the export command and then calls another .py script (and how would the first .py script look)?
No, the shell script that the Python program would invoke would be a
different process and so commands executed in it would have no effect on
the state of another.
--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Success and failure are equally disastrous.
-- Tennessee Williams This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jimmy Cracker |
last post by:
Is it completely impossible in UNIX to push an environment variable to
the parent shell? I would like to do something like this:
main(int argc, char *argv)
{
char *var;
var = (char *)malloc(1028);
strcpy(var, "VAR=");
strcat(var, argv);
|
by: Bonj |
last post by:
Hi
My application installs a front-end GUI, which runs code when buttons are
clicked. It also installs a command-line utility, that is a console
application.
They are both installed to the program's application directory, along with
the DLL that contains the generic code that they both call.
My question is, is there any way of modifying the environment variable
permanently with the setup project, such that the user has got the...
|
by: |
last post by:
Hi all, I am trying to append a certain string to the
PATH environment variable programmatically. I am able to
read what is in the variable using the System.Environment
method GetEnvironmentVariable("path"). However, I don't
know yet how to append strings to the path variable. Any
help is appreciated. Thanks a lot.
|
by: Strauss |
last post by:
Hi:
I'm setting the INCLUDE environment variable, but VC is not searching
it for header files (I've checked it with filemon). I've tried with user
variable, machine variable, rebooted the machine, nothing makes it work.
Does INCLUDE variable until work? I'm using WinXP SP1 and VC 7.1.
Thanks,
|
by: yaron |
last post by:
Hi,
my application use environment variable, let call it FOO.
how can i add the FOO environment variable to my project or my solution,
so the line
string foo = Environment.GetEnvironmentVariable("FOO");
| |
by: Joe HM |
last post by:
Hello -
I am putting together a little ConsoleApplication that is supposed to
check for an Environment Variable and create it if it does not exist.
I found some code that will add a new Environment Variable to the
Registry and send a broadcast message to all open windows to update
their environment.
When I run this *.exe out of a cmd.exe, it will not update the
environment of that shell. The new Environment Variable shows up
|
by: Stephen Cattaneo |
last post by:
Hello all,
I am attempting to execute an automated test (written in Python) via
cron. I have to check the HOSTNAME variable as part of the test, oddly
under cron the HOSTNAME environment variable is not in the os.environ
dictionary. I know that cron runs in a subshell that does not have all
of the normally set environment variables. HOSTNAME is not one of those
variables, it is set even in cron's subshell. Why doesn't python get...
|
by: Cameron Simpson |
last post by:
On 17Aug2008 21:25, John Nagle <nagle@animats.comwrote:
Because $HOSTNAME is a bash specific variable, set by bash but NOT
EXPORTED! Like $0 and a bunch of other "private" variables, subprocesses
do not inherit this value. From "man bash":
Shell Variables
The following variables are set by the shell:
|
by: vkbishnoi |
last post by:
I need to create an environment variable for another local user in windows. For example, say suppose there are 2 users in the system (User1 and User2). Currently User1 is logged in the system. Now Is there any way we can create user level environment variable for User2 using C# .Net 2.0.
If we use Environment.SetEnvironmentVariable(envName, envValue, EnvironmentVariableTarget.User) then it will create the environment variable for the...
|
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...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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...
|
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,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |