python - 如何在系统范围内可用的虚拟环境中创建脚本的入口点?
问题描述
我正在使用setuptools
python 打包,在 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 或其他一些自动方法中?
我的设置
- 操作系统:macOS Catalina
- Shell:bash(是的,我在 Catalina 更新后将其改回并抑制了烦人的“zsh is now default”消息)
- IDE:PyCharm 19.1 专业版
- Anaconda:4.4.7(注意:已被 Catalina 更新从 root 移动到 User/my_user/)
- 蟒蛇:3.7
- 虚拟环境类型:conda
解决方案
看起来conda run
能帮上忙。
我无法测试它,但看起来它可以允许编写一个更简单的 shell 脚本。像这样的东西:
#!/usr/bin/env sh
conda run -n ENV my_entry_name "$@"
不过,这似乎是一个实验性功能。
推荐阅读
- solr - Broadleaf Commerce Solr 问题
- python - Django + Python:会自动检测类型吗?
- oracle - 在 Oracle 中使用参数查询更改 SQL 查询
- powershell - 无法在更新模块上找到存储库
- php - 对象存储的创建方法
- python - 当某些字符串具有 UTF-8 字符时,pandas DataFrame.style.render
- node.js - Express req.session 已设置,然后在尝试 POST 时为空
- json - 颤振:如何从 json 中获取数据?
- mysql - 更新任何行值时的 MySQL 自动更新日期
- java - java.lang.NoSuchMethodError:Lcom/google/firebase/FirebaseApp 类中没有虚拟方法 zzbqo()Z;