简体   繁体   English

将 python package 子目录添加到系统路径

[英]Add python package subdirectory to system path

I have a python package that has the following structure:我有一个 python package 具有以下结构:

Package
├───ExternalModules
│   ├───external_module.exe
│   └───external_module_importer.py
└───setup.py

The package is installed with the setup.py file ("pip install./") and in it, there is a library/dependency that relies on the executable_module.exe to run. package 与 setup.py 文件(“pip install./”)一起安装,其中有一个依赖于 executable_module.exe 运行的库/依赖项。

I don't want my users having to download the module and add the path to it manually, so I want to have it on my package and add it to the system path automatically (during package installation or on runtime).我不希望我的用户必须下载模块并手动添加路径,所以我想将它放在我的 package 上并自动将其添加到系统路径中(在 package 安装期间或运行时)。

Currently, I have this external_module_importer.py trying to import it in the following way:目前,我有这个 external_module_importer.py 试图通过以下方式导入它:

import os
import sys

def add_module_to_path():
    filedir = os.path.dirname(os.path.abspath(__file__))
    sys.path.append(filedir)

but even if I call this function, the external dependency that relies on the.exe file gives me the following error:但即使我称之为 function,依赖 .exe 文件的外部依赖项也会给我以下错误:

FileNotFoundError: external_module not found or not executable, check the configuration file

If I manually add Package/ExternalModules/ folder to PATH, it works.如果我手动将 Package/ExternalModules/ 文件夹添加到 PATH,它可以工作。

Is there a good solution for it?有没有好的解决方案?

Answering myself:回答我自己:

When I did append to sys.path, I was adding the directory to the PYTHONPATH environment variable, not the system PATH variable.当我将 append 添加到 sys.path 时,我将目录添加到 PYTHONPATH 环境变量,而不是系统 PATH 变量。

To do de former the necessary command is this:要做到这一点,必要的命令是这样的:

os.environ["PATH"] += os.pathsep + path

This is system agnostic.这与系统无关。

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

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