473,749 Members | 2,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Set an environment variable

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
Oct 20 '05
28 20989
Grant Edwards <gr****@visi.co m> writes:
My point: the OP wanted to know how to export an environment
variable to a child process. Either of the lines of code above
will do that, so what's with all the shellular shenanigans?


Actually, the OP didn't say he wanted to export a variable to a child
process. He said he wanted to know how to do the equivalent of "export
SYMBOL=value", which got intreprted as "setting a variable in the
parent shell."

<mike
--
Mike Meyer <mw*@mired.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Oct 21 '05 #21
On 2005-10-21, Mike Meyer <mw*@mired.or g> wrote:
Grant Edwards <gr****@visi.co m> writes:
My point: the OP wanted to know how to export an environment
variable to a child process. Either of the lines of code above
will do that, so what's with all the shellular shenanigans?


Actually, the OP didn't say he wanted to export a variable to a child
process. He said he wanted to know how to do the equivalent of "export
SYMBOL=value", which got intreprted as "setting a variable in the
parent shell."


The only think you can export an environment variable to is a
child process, so I thought it obvious that was what he was
doing with the "export" command example.

--
Grant Edwards grante Yow! I'm gliding over a
at NUCLEAR WASTE DUMP near
visi.com ATLANTA, Georgia!!
Oct 21 '05 #22
On Fri, 21 Oct 2005 20:13:32 -0000, Grant Edwards <gr****@visi.co m> wrote:
On 2005-10-21, Mike Meyer <mw*@mired.or g> wrote:
Grant Edwards <gr****@visi.co m> writes:
My point: the OP wanted to know how to export an environment
variable to a child process. Either of the lines of code above
will do that, so what's with all the shellular shenanigans?
Actually, the OP didn't say he wanted to export a variable to a child
process. He said he wanted to know how to do the equivalent of "export
SYMBOL=value", which got intreprted as "setting a variable in the
parent shell."


The only think you can export an environment variable to is a
child process


Well, you know that, and I know that too. From my experience, many people don't...
so I thought it obvious that was what he was
doing with the "export" command example.


Well, when I first read the OP's message, I thought it was obvious he wanted to export the variable to the parent shell. Re-reading it, it was actually not so obvious, so my apologies for not having included an example using os.environment. ..
--
python -c "print ''.join([chr(154 - ord(c)) for c in 'U(17zX(%,5.zmz 5(17;8(%,5.Z65\ '*9--56l7+-'])"
Oct 24 '05 #23
On 2005-10-24, Eric Brunel <er*********@de spammed.com> wrote:
The only think you can export an environment variable to is a
child process


Well, you know that, and I know that too. From my experience,
many people don't...


True. Using Unix for 20+ years probably warps one's perception
of what's obvious and what isn't.

--
Grant Edwards grante Yow! My face is new, my
at license is expired, and I'm
visi.com under a doctor's care!!!!
Oct 24 '05 #24
Grant Edwards <gr****@visi.co m> wrote:
On 2005-10-24, Eric Brunel <er*********@de spammed.com> wrote:
The only think you can export an environment variable to is a
child process


Well, you know that, and I know that too. From my experience,
many people don't...


True. Using Unix for 20+ years probably warps one's perception
of what's obvious and what isn't.


This specific issue is identical in Windows, isn't it? I do not know
any OS which does have the concept of "environmen t variable" yet lets
such variables be ``exported'' to anything but a child process.
Alex
Oct 26 '05 #25
On Wed, 26 Oct 2005 07:42:19 -0700, Alex Martelli <al*****@yahoo. com> wrote:
Grant Edwards <gr****@visi.co m> wrote:
On 2005-10-24, Eric Brunel <er*********@de spammed.com> wrote:
>> The only think you can export an environment variable to is a
>> child process
>
> Well, you know that, and I know that too. From my experience,
> many people don't...


True. Using Unix for 20+ years probably warps one's perception
of what's obvious and what isn't.


This specific issue is identical in Windows, isn't it? I do not know
any OS which does have the concept of "environmen t variable" yet lets
such variables be ``exported'' to anything but a child process.


AmigaDOS, if I recall correctly. Its "ENV:" drive/namespace is global, and
that's its closest thing to Unix environment variables.

/Jorgen

--
// Jorgen Grahn <jgrahn@ Ph'nglui mglw'nafh Cthulhu
\X/ algonet.se> R'lyeh wgah'nagl fhtagn!
Oct 26 '05 #26
Jorgen Grahn <jg*********@al gonet.se> writes:
On Wed, 26 Oct 2005 07:42:19 -0700, Alex Martelli <al*****@yahoo. com> wrote:
Grant Edwards <gr****@visi.co m> wrote:
On 2005-10-24, Eric Brunel <er*********@de spammed.com> wrote:
>> The only think you can export an environment variable to is a
>> child process
> Well, you know that, and I know that too. From my experience,
> many people don't...
True. Using Unix for 20+ years probably warps one's perception
of what's obvious and what isn't.

This specific issue is identical in Windows, isn't it? I do not know
any OS which does have the concept of "environmen t variable" yet lets
such variables be ``exported'' to anything but a child process.

AmigaDOS, if I recall correctly. Its "ENV:" drive/namespace is global, and
that's its closest thing to Unix environment variables.


AmigaDOS had both global environment variables (using the ENV: device)
and local environment variables, that worked like the Unix
version. You manipulated them in a similar way in the shell, and they
had a similar API for programmers: one call with a flag to indicate
which you wanted. The ENV: device was an implementation detail that
let you save/restore the state of the global environment with file
commands. The posix calls checked the local then global variables.

Of course, this is now 10+ year old memory, and I may not RC.

<mike
--
Mike Meyer <mw*@mired.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Oct 26 '05 #27
On Wed, 26 Oct 2005 16:04:58 -0400, Mike Meyer <mw*@mired.or g> declaimed
the following in comp.lang.pytho n:

AmigaDOS had both global environment variables (using the ENV: device)
and local environment variables, that worked like the Unix
version. You manipulated them in a similar way in the shell, and they
had a similar API for programmers: one call with a flag to indicate
which you wanted. The ENV: device was an implementation detail that
let you save/restore the state of the global environment with file
commands. The posix calls checked the local then global variables.

Of course, this is now 10+ year old memory, and I may not RC.
Close enough. ENV: was a system defined logical name for a standard
system directory. The contents of that directory contained short files
(one line). The environment variable name was the file name, the
variable value, then, was the content of the file.

CLI commands for manipulating them tended to be of the order of

setenv name value
vs
set name value
for a shell local environment variable.

But AmigaOS also had those logical names mentioned, and those were
used in some places UNIX uses environment variables.

How would you like to refer to a directory?

HD0:myDirectory
SYS:myDirectory
myDir:

The first references via the hardware partition name; the second by
a volume label, and the third by a logical name (assign myDir:
SYS:myDirectory )

Windows lets one assign a volume label to a partition, but it is not
used for anything except display. Amiga volume labels were useful: one
could refer to two separate floppy disks by two different volume labels,
and the OS would prompt the user to insert the disk by name as needed.
Even more, if the need was fresh, rather than to perform I/O on an
already opened file, once could satisfy the prompt by putting the floppy
in ANY available floppy drive
<mike -- =============== =============== =============== =============== == <
wl*****@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
=============== =============== =============== =============== == <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.ne tcom.com/> <

Oct 27 '05 #28
On Wed, 26 Oct 2005 16:04:58 -0400, Mike Meyer <mw*@mired.or g> wrote:
Jorgen Grahn <jg*********@al gonet.se> writes:
On Wed, 26 Oct 2005 07:42:19 -0700, Alex Martelli <al*****@yahoo. com> wrote:
Grant Edwards <gr****@visi.co m> wrote:
On 2005-10-24, Eric Brunel <er*********@de spammed.com> wrote:
>> The only think you can export an environment variable to is a
>> child process
> Well, you know that, and I know that too. From my experience,
> many people don't...
True. Using Unix for 20+ years probably warps one's perception
of what's obvious and what isn't.
This specific issue is identical in Windows, isn't it? I do not know
any OS which does have the concept of "environmen t variable" yet lets
such variables be ``exported'' to anything but a child process. AmigaDOS, if I recall correctly. Its "ENV:" drive/namespace is global, and
that's its closest thing to Unix environment variables.


AmigaDOS had both global environment variables (using the ENV: device)
and local environment variables, that worked like the Unix
version.


As I recalled it, the latter type was shell-local and not accessible to
normal processes ...
You manipulated them in a similar way in the shell, and they
had a similar API for programmers: one call with a flag to indicate
which you wanted.
.... but if there were system calls to access them, I must have remembered
incorrecly. Possibly I was too stupid back then to find enviroment variables
very useful ;-)
Of course, this is now 10+ year old memory, and I may not RC.


I think I remember /you/ though, from the Amiga newsgroups in the early
nineties. And now I feel old -- and offtopic.

/Jorgen

--
// Jorgen Grahn <jgrahn@ Ph'nglui mglw'nafh Cthulhu
\X/ algonet.se> R'lyeh wgah'nagl fhtagn!
Oct 27 '05 #29

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

Similar topics

13
3024
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);
1
2459
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...
4
39098
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.
3
5685
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,
6
5337
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");
0
1980
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
4
7557
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...
0
3474
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:
4
16590
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...
0
8997
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
9568
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
9256
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
8257
agi2029
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...
1
6801
isladogs
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...
0
6079
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();...
1
3320
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
2
2794
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2218
bsmnconsultancy
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...

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.