简体   繁体   中英

Python Modify OS Path Variable

I am going to try and say this right but it's a bit outside my area of expertise.

I am using the xgboost library in a windows environment with Python 2.7 which requires all kinds of nasty compiling and installation.

That done, the instructions I'm following tell me I need to modify the OS Path Variable in an iPython notebook before I actually import the library for use.

The instructions tell me to run the following:

import os

mingw_path = 'C:\\Program Files\\mingw-w64\\x86_64-5.3.0-posix-seh-rt_v4-rev0\\mingw64\\bin'

os.environ['PATH'] = mingw_path + ';' + os.environ['PATH']

then I can import

import xgboost as xgb
import numpy as np
....

This works. My question. Does the OS path modification make a permanent change in the path variable or do I need to modify the os path variable each time I want to use it as above?

Thanks in advance.

EDIT

Here is a link to the instructions I'm following. The part I'm referencing is toward the end.

The os.environ function is only inside the scope of the python/jupyter console:

Here's evidence of this in my bash shell:

$ export A=1
$ echo $A
1
$ python -c "import os; print(os.environ['A']); os.environ['A'] = '2'; print(os.environ['A'])"
1
2
$ echo $A
1

The python line above, prints the environ variable A and then changes it's value and prints it again.

So, as you see, any os.environ variable is changed within the python script, but when it gets out, the environment of the bash shell does not change.

Another way of doing this is to modify your User or System PATH variable. But this may break other things because what you're doing may replace the default compiler with mingw and complications may arise. I'm not a windows expert, so not sure about that part.

In a nutshell:

  • The os.environ manipulations are local only to the python process
  • It won't affect any other program
  • It has to be done every time you want to import xgboost

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM