Re: set environmental variable from python

2014-11-01 Thread Alexander Blinne
Am 31.10.2014 um 02:22 schrieb Artur Bercik:
> I have to set environmental variable in my windows PC as follows:
> 
> variable name: GISBASE
> 
> value: C:\GRASS-64
> 
> Is it possible to set it from python?
> 
> import sys
> 
> sys.path.append("C:\\GRASS-64")
> 
> But how to give variable name? I have to set both the variable name and
> value.

http://lmgtfy.com/?q=how+to+set+environment+variable+with+python

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: set environmental variable from python

2014-10-31 Thread Zachary Ware
On Fri, Oct 31, 2014 at 1:11 PM, Dennis Lee Bieber
 wrote:
> On Thu, 30 Oct 2014 22:00:33 -0500, Zachary Ware
>  declaimed the following:
> >From a Command Prompt, do 'help setx' for details on how to use setx.
>
> Really? 
>
> C:\Users\Wulfraed\Documents>help setx
> This command is not supported by the help utility.  Try "setx /?".

Oops, should have tried it before posting.  It at least tells you how
to get what I meant to give, though ;)

> OTOH: I didn't know about this command before -- and setting user 
> level
> environment variables on Win7 wasn't working for me...

I came across it after becoming very frustrated with having to jump
through all the hoops to find the environment variable GUI at one
point.

-- 
Zach
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: set environmental variable from python

2014-10-31 Thread Mark Lawrence

On 31/10/2014 02:36, Rustom Mody wrote:

On Friday, October 31, 2014 8:01:08 AM UTC+5:30, Zachary Ware wrote:

On Thursday, October 30, 2014, Artur Bercik  wrote:

Dear Dave Angel


Thanks for your answer.


I am using Python 2.7


I want to set it permanently.
I have to set several variables so it would be easier if I could set them from 
Python.


Depending on how "permanently" you mean, about your only solutions
would be "os.system('setx <...>')" or manually manipulating the
registry with the _winreg module.


Or dont do it from python but directly with regedit??

The question really is: Why do you wish to do this from within python?




Or do it directly with something like the Rapid Environment Editor 
http://www.rapidee.com/en/about


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

--
https://mail.python.org/mailman/listinfo/python-list


Re: set environmental variable from python

2014-10-31 Thread Dave Angel

On 10/30/2014 10:40 PM, Artur Bercik wrote:


On Fri, Oct 31, 2014 at 11:30 AM, Zachary Ware <
zachary.ware+pyl...@gmail.com> wrote:


On Thursday, October 30, 2014, Artur Bercik  wrote:


Dear Dave Angel

Thanks for your answer.

I am using Python 2.7

I want to set it permanently.
I have to set several variables so it would be easier if I could set them
from Python.



Depending on how "permanently" you mean, about your only solutions would
be "os.system('setx <...>')" or manually manipulating the registry with the
_winreg module.





> could you please elaborate 'setx <...>'?
>

SETX is a Windows utility.  I don't run Windows, but I found a Win7 
machine and played again.  I used to be quite knowledgeable about 
Windows, but I could get parts of the following wrong.


To see what SETX does, type (at a cmd line):

SETX /?

Or google it.  (I used duckduckgo, and here are the first two useful 
links:  Obviously the second one is an official Microsoft page.


http://ss64.com/nt/setx.html
http://technet.microsoft.com/en-us/library/cc755104.aspx

There are several variants of "permanent" that are relevant here.

1) Nothing you change in one process will change another process that's 
already running.  That includes any shells you might have already 
started, such as DOS boxes.  (cmd windows)  And the term shell here also 
includes any other already-running process that is going to launch 
another program.  The shell controls its child's environment, and in 
general do not check the registry for changes you might have made.


2) SETX will affect new processes launched directly from Windows (eg. 
from Explorer), because it changes the registry itself.


3) SETX will affect *new* shells launched, and thus indirectly affect 
their children.


4) SETX effects will survive a reboot.  So when you restart your system, 
you'll still see the new values.  Incidentally, that's the first time 
when you'll be sure that all processes will see the new values.


5) SETX can be used on remote (networked) systems, and the rules are 
somewhat different there.


6) If you have (or ever will have) multiple users, you have to consider 
whether you want to affect the system environment variables or just one 
user.  And if just one user, whether it's yourself or some other 
account, such as Administrator.  Every time a process is launched by the 
system, it builds an environment from TWO sets of registry items.  So if 
you want to affect all users, you need to change the system variables.



If you are just going to do this once, and you want to affect all the 
processes that ever run in the future, you'll be much safer using the 
control panel:


Control Panel | System | Advanced | Environment Variables




--
DaveA
--
https://mail.python.org/mailman/listinfo/python-list


Re: set environmental variable from python

2014-10-31 Thread Gisle Vanem

"Artur Bercik"  wrote:


I have to set several variables so it would be easier if I could set them
from Python.


You can write a Python-script that generates a .bat file (in your %TEMP-dir?).
And run this .bat file when Python ends. Put it all in an alias or another .bat
file. E.g. untested:
alias set_env=python  -o %TEMP%\set_env.bat & %TEMP%\set_env.bat

--gv

--
https://mail.python.org/mailman/listinfo/python-list


Re: set environmental variable from python

2014-10-30 Thread Zachary Ware
On Thu, Oct 30, 2014 at 9:40 PM, Artur Bercik  wrote:
> could you please elaborate 'setx <...>'?

>From a Command Prompt, do 'help setx' for details on how to use setx.

Rustom's suggestion of using regedit is going to be far easier than
using _winreg (which probably shouldn't even be considered as an
option).  Using `os.system('setx ...')` is going to be the easiest way
to do things if you have to calculate the values of your variables,
but if you just have a bunch of values that you're going to have to
key in anyway, just use setx directly (or in a batch script).

-- 
Zach
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: set environmental variable from python

2014-10-30 Thread Rustom Mody
On Friday, October 31, 2014 7:33:43 AM UTC+5:30, Artur Bercik wrote:
> Dear Dave Angel
> 
> 
> Thanks for your answer.
> 
> 
> I am using Python 2.7
> 
> 
> I want to set it permanently.
> I have to set several variables so it would be easier if I could set them 
> from Python.

regedit is scriptable
http://support.microsoft.com/kb/310516

[Be careful though! Follow the precautions like backing up the registry
before messing around]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: set environmental variable from python

2014-10-30 Thread Artur Bercik
I have to set several variables so it would be easier if I could set them
from Python.

On Fri, Oct 31, 2014 at 11:36 AM, Rustom Mody  wrote:

> On Friday, October 31, 2014 8:01:08 AM UTC+5:30, Zachary Ware wrote:
> > On Thursday, October 30, 2014, Artur Bercik  wrote:
> >
> > Dear Dave Angel
> >
> >
> > Thanks for your answer.
> >
> >
> > I am using Python 2.7
> >
> >
> > I want to set it permanently.
> > I have to set several variables so it would be easier if I could set
> them from Python.
> >
> >
> > Depending on how "permanently" you mean, about your only solutions
> > would be "os.system('setx <...>')" or manually manipulating the
> > registry with the _winreg module.
>
> Or dont do it from python but directly with regedit??
>
> The question really is: Why do you wish to do this from within python?
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: set environmental variable from python

2014-10-30 Thread Artur Bercik
could you please elaborate 'setx <...>'?


On Fri, Oct 31, 2014 at 11:30 AM, Zachary Ware <
zachary.ware+pyl...@gmail.com> wrote:

> On Thursday, October 30, 2014, Artur Bercik  wrote:
>
>> Dear Dave Angel
>>
>> Thanks for your answer.
>>
>> I am using Python 2.7
>>
>> I want to set it permanently.
>> I have to set several variables so it would be easier if I could set them
>> from Python.
>>
>
> Depending on how "permanently" you mean, about your only solutions would
> be "os.system('setx <...>')" or manually manipulating the registry with the
> _winreg module.
>
> Hope this helps,
> --
> Zach
>
>
> --
> Sent from Gmail Mobile
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: set environmental variable from python

2014-10-30 Thread Rustom Mody
On Friday, October 31, 2014 8:01:08 AM UTC+5:30, Zachary Ware wrote:
> On Thursday, October 30, 2014, Artur Bercik  wrote:
> 
> Dear Dave Angel
> 
> 
> Thanks for your answer.
> 
> 
> I am using Python 2.7
> 
> 
> I want to set it permanently.
> I have to set several variables so it would be easier if I could set them 
> from Python.
> 
> 
> Depending on how "permanently" you mean, about your only solutions
> would be "os.system('setx <...>')" or manually manipulating the
> registry with the _winreg module.

Or dont do it from python but directly with regedit??

The question really is: Why do you wish to do this from within python?


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: set environmental variable from python

2014-10-30 Thread Zachary Ware
On Thursday, October 30, 2014, Artur Bercik  wrote:

> Dear Dave Angel
>
> Thanks for your answer.
>
> I am using Python 2.7
>
> I want to set it permanently.
> I have to set several variables so it would be easier if I could set them
> from Python.
>

Depending on how "permanently" you mean, about your only solutions would be
"os.system('setx <...>')" or manually manipulating the registry with the
_winreg module.

Hope this helps,
--
Zach


-- 
Sent from Gmail Mobile
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: set environmental variable from python

2014-10-30 Thread Artur Bercik
Dear Dave Angel

Thanks for your answer.

I am using Python 2.7

I want to set it permanently.
I have to set several variables so it would be easier if I could set them
from Python.

Hearing the solution.

On Fri, Oct 31, 2014 at 10:50 AM, Dave Angel  wrote:

> On 10/30/2014 09:22 PM, Artur Bercik wrote:
>
>> I have to set environmental variable in my windows PC as follows:
>>
>> variable name: GISBASE
>>
>> value: C:\GRASS-64
>>
>> Is it possible to set it from python?
>>
>
> Which Python?  I'll have to assume 3.x
>
>
>> import sys
>>
>> sys.path.append("C:\\GRASS-64")
>>
>> But how to give variable name? I have to set both the variable name and
>> value.
>>
>>
> sys.path has nothing to do with an environment variable of GISBASE.
>
> Instead you could look up os.environ at:
>
>   https://docs.python.org/3/library/os.html
>
> Also see os.getenv and os.setenv.
>
>
>
> Note that it's not necessarily supported.  But I believe it is for a
> standard build on Windows.
>
> Next question is what you hope to achieve by setting such a variable. You
> do realize that it will vanish again when your python process ends?  So if
> you're just planning to use it in your own code, I'd recommend finding
> another method of saving the name & value.
>
> The only value I can see is if you plan to create a subprocess from your
> Python code.
>
> --
> DaveA
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: set environmental variable from python

2014-10-30 Thread Dave Angel

On 10/30/2014 09:22 PM, Artur Bercik wrote:

I have to set environmental variable in my windows PC as follows:

variable name: GISBASE

value: C:\GRASS-64

Is it possible to set it from python?


Which Python?  I'll have to assume 3.x



import sys

sys.path.append("C:\\GRASS-64")

But how to give variable name? I have to set both the variable name and
value.



sys.path has nothing to do with an environment variable of GISBASE.

Instead you could look up os.environ at:

  https://docs.python.org/3/library/os.html

Also see os.getenv and os.setenv.



Note that it's not necessarily supported.  But I believe it is for a 
standard build on Windows.


Next question is what you hope to achieve by setting such a variable. 
You do realize that it will vanish again when your python process ends? 
 So if you're just planning to use it in your own code, I'd recommend 
finding another method of saving the name & value.


The only value I can see is if you plan to create a subprocess from your 
Python code.


--
DaveA
--
https://mail.python.org/mailman/listinfo/python-list


set environmental variable from python

2014-10-30 Thread Artur Bercik
I have to set environmental variable in my windows PC as follows:

variable name: GISBASE

value: C:\GRASS-64

Is it possible to set it from python?

import sys

sys.path.append("C:\\GRASS-64")

But how to give variable name? I have to set both the variable name and
value.

Thanks in the advance.

Artur
-- 
https://mail.python.org/mailman/listinfo/python-list