Hallo,
ich möchte in einer Python Function eine Environment Variable setzen z.B.
def myfunc() :
import os
os.environ['myvariable'] = 'abc'
Diese Variable möchte ich anschließend von einem Fortran-Programm, das unter Python gestartet wird lesen mit
call getenv('myvariable',myvar)
Das Fortran-Pogramm kann jedoch myvariable nicht finden und bringt in myvar einen leeren Zeichenstring zurück. Ich vermute, dass myvariable nur lokal gestzt wird und beim Verlassen der Function nicht mehr definiert ist.
Gibt es eine Möglichkeit, aus der Python-Function heraus eine Environment-Variable global zu setzen, so dass sie auch nach verlassen der Function oder am Besten unter Linux mit
echo $myvariable
zu sehen ist?
globale Environment Variable
- cofi
- Python-Forum Veteran
- Beiträge: 4432
- Registriert: Sonntag 30. März 2008, 04:16
- Wohnort: RGFybXN0YWR0
Nein, das geht so nicht. Environment-Variablen sind an ihre Prozesse gebunden, du kannst aber das Fortran-Programm aus Python starten, so dass es das Environment erbt.
Michael Markert ❖ PEP 8 Übersetzung ❖ Tutorial Übersetzung (3.x) ⇒ Online-Version (Python 3.3) ❖ Deutscher Python-Insider ❖ Projekte
Code: Alles auswählen
subprocess.call(["python", "-c", "import os;print(os.environ)"], env={"foo": "bar"})
@sma:
Ohne $PATH funktioniert der Aufruf nicht ...
Code: Alles auswählen
env = dict(os.environ)
env['foo'] = 'bar'
subprocess.call(["python", "-c", "import os;print(os.environ)"], env=env)
Bei mir geht's:
Stefan
Code: Alles auswählen
sma$ python
Python 2.6.5 (r265:79063, May 29 2010, 13:19:44)
[GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.call(["python", "-c", "import os;print(os.environ)"], env={"foo": "bar"})
{'__CF_USER_TEXT_ENCODING': '0x1F5:0:3', 'foo': 'bar', 'VERSIONER_PYTHON_PREFER_32_BIT': 'no', 'VERSIONER_PYTHON_VERSION': '2.6'}
0
ich würde "python" im subprocess-call mit sys.executable ersetzten
the more they change the more they stay the same