问题
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
回答1:
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.
来源:https://stackoverflow.com/questions/58436993/how-do-you-make-an-entry-point-to-a-script-in-a-virtual-environment-available-sy