简体   繁体   English

如何在 Windows 10 中为 python 添加 os.environ[“PATH”] 的路径?

[英]How to add path to os.environ[“PATH”] for python in Windows 10?

I have a script which has an error because 'neato.exe' can't be found in paths.我的脚本有错误,因为在路径中找不到“neato.exe”。 When I look at os.environ["PATH"], indeed C:\\Program Files (x86)\\Graphviz2.38\\bin , the path to neato.exe , is not there.当我查看 os.environ["PATH"] 时,确实不存在C:\\Program Files (x86)\\Graphviz2.38\\bin的路径, neato.exe不存在。 I can do a hack for the time being by adding this line, but this doesn't seem satisfactory.我暂时可以通过添加这一行来进行破解,但这似乎并不令人满意。

if  not 'C:\\Program Files (x86)\\Graphviz2.38\\bin' in os.environ["PATH"]: 
    os.environ["PATH"] += os.pathsep + 'C:\\Program Files (x86)\\Graphviz2.38\\bin' 

Nonetheless, it shows that the error ValueError("Program %s not found in path." neato.exe) is an accurate error.尽管如此,它表明错误ValueError("Program %s not found in path." neato.exe)是一个准确的错误。 The script works when I add the path to Neato.当我将路径添加到 Neato 时,该脚本有效。 I added C:\Program Files (x86)\Graphviz2.38\bin to my Environmental Variables in windows, but to no avail.我将C:\Program Files (x86)\Graphviz2.38\bin添加到 windows 的环境变量中,但无济于事。 And I also noticed that there are only a few paths in my Path Env.而且我还注意到我的路径环境中只有几条路径。 Vars., not the many that python lists. Vars.,不是 python 列出的很多。 I am using python 3.7 and also running it using the anaconda navigator.我正在使用 python 3.7 并使用 anaconda 导航器运行它。 I would like to make a more permanent change so I don't have to edit every script that looks for neato.exe with the silly if statement above.我想进行更永久的更改,这样我就不必使用上面愚蠢的if statement编辑每个查找neato.exe的脚本。 Does anyone know how to change what's in os.environ["PATH"] in anaconda?有谁知道如何更改 anaconda 中os.environ["PATH"]中的内容?

I am using networkx, networkx.drawing.nx_agraph.to_agraph.我正在使用networkx,networkx.drawing.nx_agraph.to_agraph。 The script agraph.py has this function ( _which() ), which need to make a path match or it will throw an error.脚本agraph.py有这个 function ( _which() ),需要进行路径匹配,否则会抛出错误。

def _which(self, name):
    """Searches for name in exec path and returns full path"""


    import os
    import glob

    paths = os.environ["PATH"]

    if os.name == "nt":
        exe = ".exe"
    else:
        exe = ""
    for path in paths.split(os.pathsep):

        match = glob.glob(os.path.join(path, name + exe))
        if match:
            return match[0]
    raise ValueError("No prog %s in path." % name)

A few things to note about Windows Path:关于 Windows 路径需要注意的几点:

  1. Windows Path is a combination of user defined and system defined - the former is appended to the latter Windows 路径是用户定义和系统定义的组合 - 前者附加到后者
  2. Windows Path has a pretty short length limit Windows 路径的长度限制很短

Unfortunately because of the above, you'll probably want to temporarily change the Path for your program to ensure it can find the binary you're looking for.不幸的是,由于上述原因,您可能需要临时更改程序的路径以确保它可以找到您正在寻找的二进制文件。

You could set the PATH environment variable with the Graphviz bin directory at the beginning before executing your script(s).在执行脚本之前,您可以在开始时使用 Graphviz bin 目录设置PATH环境变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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