如何在系统范围内可用的虚拟环境中为脚本提供入口点? [英] How do you make an entry point to a script in a virtual environment available system-wide?

查看:126
本文介绍了如何在系统范围内可用的虚拟环境中为脚本提供入口点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

setup.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现在是默认值"消息)
  • IDE:PyCharm 19.1 Pro
  • Anaconda:4.4.7(注意:由于Catalina更新已从根目录移至User/my_user/)
  • Python:3.7
  • 虚拟环境类型:conda

解决方案

类似 conda run 可以帮助您.

我无法对其进行测试,但是看起来它可以允许编写简单得多的shell脚本.像这样的东西:

#!/usr/bin/env sh

conda run -n ENV my_entry_name "$@"

不过似乎是实验性功能.

I'm using setuptools for python packaging where I define console script entry points the usual way in the setup.py file:

setup.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'
                                      ]},
     ...
)

After installing the package, I can call this entry point from a batch file like this:

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>

While this works, the use of the virtual environment is causing me to include all the path info before the entry point call, which in my view really destroys the simplicity an entry point is supposed to provide to the consumer of a script. Is there a way to get setuptools to register the entry point system-wide so that I can call the entry point without the path like this?:

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>

Without this complication introduced by the virtual environments, a console script entry point lets the script consumer use a script without having to know where the script is installed or even what language it's written in. I'd like to preserve this simplicity even when packaging in virtual environments.

What I've tried - I located the actual entry point file in the virtual environment after installing the package:

/anaconda3/envs/my_env/bin/my_entry_name

and placed a copy of this file in the main bin path:

/anaconda3/bin/my_entry_name

and found that I can then call the entry point without the path, as desired, however this is a manual step I don't want to make script consumers to do. Is there a way to get setuptools to place the entry point file in the general bin path rather than the environment bin or some other automatic means to this end?

My setup

  • OS: macOS Catalina
  • Shell: bash (yes, I changed it back after Catalina update and suppressed the nagging 'zsh is now default' message)
  • IDE: PyCharm 19.1 Pro
  • Anaconda: 4.4.7 (note: was moved from root to User/my_user/ by Catalina update)
  • Python: 3.7
  • Virtual env type: conda

解决方案

Looks like conda run could help.

I can't test it, but looks like it could allow to write a much simpler shell script. Something like that:

#!/usr/bin/env sh

conda run -n ENV my_entry_name "$@"

Seems to be an experimental feature though.

这篇关于如何在系统范围内可用的虚拟环境中为脚本提供入口点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆