Saving parameters between Python applications?

S

Stodge

I'm trying to do the following. I have a Python application that is
run:

python app1.py --location=c:\test1

What I want to do is save the location parameter, so I can then do (in
the same window):

python app2.py

And have app2.py automatically have access to the value of "location".

Now, the difficult part is, that in another window I want to do:

python app1.py --location=c:\test2
python app2.py

And have app2.py automatically get c:\test2 as the location. So the
two windows (consoles) are isolated from each other.

I thought I could use os.environ, but that doesn't save the variable
for applications that are run afterwards in the same window.

Any suggestions?

Thanks
 
S

Sebastian Bassi

python app1.py --location=c:\test1
What I want to do is save the location parameter, so I can then do (in
the same window):
python app2.py
And have app2.py automatically have access to the value of "location".

Do app1.py to save a pickle of the value you want app2 to read.
 
S

Stodge

Good idea, but I can't guarantee that the two scripts will be run from
the same directory - so where to store the pickle?
 
S

Sebastian Bassi

Good idea, but I can't guarantee that the two scripts will be run from
the same directory - so where to store the pickle?

It doesn't matter if is the same directory or not, as long as both
programs has access to the pickle file (one program should have write
access and the other program should have at least read access).
 
L

Laurent Pointal

Stodge a écrit :
I'm trying to do the following. I have a Python application that is
run:

python app1.py --location=c:\test1

What I want to do is save the location parameter, so I can then do (in
the same window):

python app2.py

And have app2.py automatically have access to the value of "location".

Now, the difficult part is, that in another window I want to do:

python app1.py --location=c:\test2
python app2.py

And have app2.py automatically get c:\test2 as the location. So the
two windows (consoles) are isolated from each other.

I thought I could use os.environ, but that doesn't save the variable
for applications that are run afterwards in the same window.

Any suggestions?

May use simple file in known place:
$HOME/.myprefs
$HOME/.conf/myprefs

Or host specific configuration API:
WindowsRegistry HKEY_CURRENT_USER\Software\MySociety\MyApp\myprefs


See os.getenv, and _winreg Windows specific module.
See also standard ConfigParser module
Hope you know how to read/write files.
 
B

Bruno Desthuilliers

Stodge a écrit :
I'm trying to do the following. I have a Python application that is
run:

python app1.py --location=c:\test1

What I want to do is save the location parameter, so I can then do (in
the same window):

python app2.py

And have app2.py automatically have access to the value of "location".

Now, the difficult part is, that in another window I want to do:

python app1.py --location=c:\test2
python app2.py

And have app2.py automatically get c:\test2 as the location. So the
two windows (consoles) are isolated from each other.

I thought I could use os.environ, but that doesn't save the variable
for applications that are run afterwards in the same window.

Any suggestions?

Yes : pass the same arg to both app1.py and app2.py !-)

Braindead, I know, but still the simplest solution.
 
S

Stodge

You're probably right!

Thanks all. :)

Stodge a écrit :













Yes : pass the same arg to both app1.py and app2.py !-)

Braindead, I know, but still the simplest solution.
 
B

bryanjugglercryptographer

On Sep 17, 6:39 am, Laurent Pointal
May use simple file in known place:
$HOME/.myprefs
$HOME/.conf/myprefs

Or host specific configuration API:
WindowsRegistry HKEY_CURRENT_USER\Software\MySociety\MyApp\myprefs

See os.getenv, and _winreg Windows specific module.
See also standard ConfigParser module


Also, os.path offers expanduser(). The following is reasonably
portable:

import os

user_home_dir = os.path.expanduser("~")
 
S

Stodge

os.path.expanduser isn't an option; I need each console/window to
maintain different values which I wouldn't get from saving to a user's
home directory. Unless I used a different file for each console/window
but that just gets me into the same situation I'm already in. I think
the only option is to set environment variables using another script.
I'm really surprised and disapponited by this.

One option I thought of but haven't investigated, is the ability to
get the parent (i.e. console's) process id and use that to create a
file somewhere. Not sure if this is even possible.
 
B

Bruno Desthuilliers

Stodge a écrit :
os.path.expanduser isn't an option; I need each console/window to
maintain different values which I wouldn't get from saving to a user's
home directory. Unless I used a different file for each console/window
but that just gets me into the same situation I'm already in. I think
the only option is to set environment variables using another script.
I'm really surprised and disapponited by this.

Note that it's *not* a Python issue. You'd have the same problem with
any other language.
 
D

Diez B. Roggisch

Stodge said:
os.path.expanduser isn't an option; I need each console/window to
maintain different values which I wouldn't get from saving to a user's
home directory. Unless I used a different file for each console/window
but that just gets me into the same situation I'm already in. I think
the only option is to set environment variables using another script.
I'm really surprised and disapponited by this.

you can't do that either. It's the principle behind environment-vars that
you can't alter the ones of your parent process.

That's the reason why environment-changes must be done by using "source":


# source my_script

where my_script contains e.g.

export FOO="bar"


If you can have your user alter his/her .bashrc, you might consider creating
a environtment-variable in there that the subsequent script invocations
refer to. Like this:

# .bashrc
export SESSION=<create some unique name using e.g. date>

Then in the python-scripts you can use SESSION as a common prefix
into /tmp-residual files or such thing.

Diez
 
S

Steve Holden

Stodge said:
os.path.expanduser isn't an option; I need each console/window to
maintain different values which I wouldn't get from saving to a user's
home directory. Unless I used a different file for each console/window
but that just gets me into the same situation I'm already in. I think
the only option is to set environment variables using another script.
I'm really surprised and disapponited by this.
That's a sign of your inexperience, then. As someone has already pointed
out, this is nothing to do with Python.

Under UNIX/Linux you could use the $$ variable to construct a filename
specific to a particular shell process and put it in the environment,
but I'm not aware of a similar feature in Windows. This is probably a
sign of *my* inexperience :)
One option I thought of but haven't investigated, is the ability to
get the parent (i.e. console's) process id and use that to create a
file somewhere. Not sure if this is even possible.
You might be able to write a Python program to access it :)

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden

Sorry, the dog ate my .sigline
 
S

Stodge

I wrote a small C program in Linux and used setenv() from stdlib and
it modified the console's environment. I can also modify the console's
environment from a DOS batch file, so why not in Python?

Guess I'm inexperienced and I just don't get it. :)
 
D

David

I'm trying to do the following. I have a Python application that is
run:

python app1.py --location=c:\test1

What I want to do is save the location parameter, so I can then do (in
the same window):

python app2.py

And have app2.py automatically have access to the value of "location".

Now, the difficult part is, that in another window I want to do:

python app1.py --location=c:\test2
python app2.py

And have app2.py automatically get c:\test2 as the location. So the
two windows (consoles) are isolated from each other.

I thought I could use os.environ, but that doesn't save the variable
for applications that are run afterwards in the same window.

Use a value based on your current tty. I don't know how to get this
value in Python but you could just shell 'tty' and grab the output.

Or, to get your parent process's pid, use os.getppid().

Then, use a temporary file whose name is based on the above info.
 
S

Stodge

os.getppid() isn't cross platform. I don't think it works on Windows.
I think I'll just create a simple shell script (BAT or Bash) for each
platform as needed.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top