在Python中如何添加到Windows路径变量以查找可执行文件

您所在的位置:网站首页 怎么找到python安装路径 在Python中如何添加到Windows路径变量以查找可执行文件

在Python中如何添加到Windows路径变量以查找可执行文件

2023-03-22 09:28| 来源: 网络整理| 查看: 265

百度翻译此文   有道翻译此文 问题描述

I have been looking for a way to make python search in folders that may not be listed in the windows path. This way I can account for some non standard locations for installed apps if they decide not to install it under c:\program files. for instance if unix utils is installed on a d drive or something.

Rather than modifying the windows path all the time. I figured I could read the registry find the path it was installed in and then add that to the search path for that python instance and still find what I'm looking for. The issue is that I also need to take into account predefined subfolders that have standard naming also.

I have searched and most of what I find is related to modifying the python path and were python finds it modules and this is not really useful for this.

I have also some across sys.path.append but can't really find anything about whether or not this would work for this. I also read the documentation for os.path and it looked like join may work but it didn't really seem to fit for this need.

Is there something that I'm just not getting here or is this not possible to do.

One last thing it needs to be cross platform compatible. I can find my own paths and add them as needed but just need a way to add to the path.

Thanks

推荐答案

To temporarily add to the path, for example:

import os import sys if sys.platform == 'win32': sep = ';' else: sep = ':' os.environ['PATH'] += sep + r'"C:\QA\Hello World"'

Note that on Windows environment variables are not case sensitive, but on UNIX they are. This will only add the path for this process, and it's children.

When you come to run the program, you need to use a shell which will use %PATH% (or $PATH).

import subprocess proc = subprocess.Popen('hello.exe',shell=True) proc.wait()


【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3