python使用变量表示决定路径出错_Python修改OS路径变量

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值