首页 > 解决方案 > 如何在系统范围内可用的虚拟环境中创建脚本的入口点?

问题描述

我正在使用setuptoolspython 打包,在 setup.py 文件中以通常的方式定义控制台脚本入口点:

安装程序.py

# -*- coding: utf-8 -*-

from setuptools import setup, find_packages

setup(...
      name='my_project',
      entry_points={'console_scripts':['my_entry_name=my_package.scripts.my_python_script:main'
                                      ]},
     ...
)

安装包后,我可以从批处理文件中调用这个入口点,如下所示:

my_CURRENT_batch_file.command

#!/bin/bash
cd "$(dirname "$0")"  # set the working directory as the command file locations

~/anaconda3/envs/my_env_name/bin/entry_point_name <my script args>

虽然这可行,但虚拟环境的使用导致我在入口点调用之前包含所有路径信息,在我看来,这确实破坏了入口点应该为脚本使用者提供的简单性。有没有办法在setuptools系统范围内注册入口点,以便我可以在没有这样路径的情况下调用入口点?:

my_DESIRED_batch_file.command

#!/bin/bash
cd "$(dirname "$0")"  # set the working directory as the command file locations

entry_point_name <my script args>

如果没有虚拟环境引入的这种复杂性,控制台脚本入口点可以让脚本使用者使用脚本,而无需知道脚本的安装位置,甚至它是用什么语言编写的。即使在打包时,我也想保持这种简单性虚拟环境。

我尝试过的-安装软件包后,我在虚拟环境中找到了实际的入口点文件:

/anaconda3/envs/my_env/bin/my_entry_name

并将此文件的副本放在主 bin 路径中:

/anaconda3/bin/my_entry_name

并发现我可以根据需要在没有路径的情况下调用入口点,但是这是我不想让脚本使用者执行的手动步骤。有没有办法setuptools将入口点文件放置在通用 bin 路径而不是环境 bin 或其他一些自动方法中?

我的设置

标签: pythonsetuptoolsvirtualizationpackagingentry-point

解决方案


看起来conda run能帮上忙。

我无法测试它,但看起来它可以允许编写一个更简单的 shell 脚本。像这样的东西:

#!/usr/bin/env sh

conda run -n ENV my_entry_name "$@"

不过,这似乎是一个实验性功能。


推荐阅读