Python Modify OS Path Variable

天涯浪子 提交于 2019-12-12 03:42:25

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/40540728/python-modify-os-path-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!