Python修改OS路径变量 [英] Python Modify OS Path Variable

查看:143
本文介绍了Python修改OS路径变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将尝试说正确的话,但这有点超出我的专业领域.

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

我在带有Python 2.7的Windows环境中使用xgboost库,它需要进行各种讨厌的编译和安装.

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

完成后,我要遵循的说明告诉我,在实际导入要使用的库之前,我需要先在iPython笔记本中修改OS Path变量.

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']

然后我可以导入

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

这有效.我的问题.操作系统路径修改是否会在路径变量中进行永久的更改,还是每次我想如上所述使用它时都需要修改os路径变量?

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?

谢谢.

编辑

这里是指令我正在关注.我要引用的部分即将结束.

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

推荐答案

os.environ函数仅在python/jupyter控制台的范围内:

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

这是我bash外壳中的证据:

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

上面的python行打印环境变量A,然后更改其值并再次打印.

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

因此,如您所见,任何os.environ变量都在python脚本中更改,但是当它退出时,bash shell的环境不会更改.

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.

执行此操作的另一种方法是修改用户或系统PATH变量.但这可能会破坏其他事情,因为您正在执行的操作可能会用mingw替换默认的编译器,并且可能会产生复杂性.我不是Windows专家,所以不确定那部分.

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.

简而言之:

  • os.environ操作仅在python进程中是本地的
  • 它不会影响任何其他程序
  • 每次要导入xgboost时都必须这样做

这篇关于Python修改OS路径变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆