Python 具有sudo权限的setuptools中的安装后脚本

Python 具有sudo权限的setuptools中的安装后脚本,python,setuptools,systemd,Python,Setuptools,Systemd,我正在尝试构建一个setup.py文件,以将python脚本作为服务安装。到目前为止,我可以安装脚本并在systemd中手动添加一个单元,并启用/启动该服务,但我想自动完成。我的问题是,构建单元文件的bash脚本需要sudo权限,我不想用sudo运行pip安装 创建_service.sh 单位档案 请注意,目前,单元文件假定包安装在名为dspt的conda虚拟环境中 bash脚本create_service.sh显然需要sudo权限,我不知道该怎么做。可能重复的have not seen The

我正在尝试构建一个setup.py文件,以将python脚本作为服务安装。到目前为止,我可以安装脚本并在systemd中手动添加一个单元,并启用/启动该服务,但我想自动完成。我的问题是,构建单元文件的bash脚本需要sudo权限,我不想用sudo运行pip安装

创建_service.sh

单位档案

请注意,目前,单元文件假定包安装在名为dspt的conda虚拟环境中


bash脚本create_service.sh显然需要sudo权限,我不知道该怎么做。

可能重复的have not seen The first post…可能重复的have not seen The first post。。。
import os
import subprocess
from setuptools import setup, Extension
import setuptools.command.install
from distutils.command.build import build as build_orig


class install(setuptools.command.install.install):
    def run(self):
        setuptools.command.install.install.run(self)
        current_dir_path = os.path.dirname(os.path.realpath(__file__))
        create_service_script_path = os.path.join(current_dir_path, 'bin', 'create_service.sh')
        subprocess.check_output([create_service_script_path])


setup(name='dspt',
      version=dspt.__version__,
      packages=['dspt'],
      include_package_data=True,
      package_data={"": ["data/template.xml"]},
      ext_modules=[ext],
      install_requires=['slackclient==1.3.1',
                        'numpy',
                        'numba',
                        'scipy',
                        'pytz',
                        'requests',
                        'pyjwt',
                        'pysmb',
                        'boto3'],
      cmdclass={
          'install': install
      },
      entry_points={'console_scripts': ['run_dspt = dspt.batch_processing:main']}
)
#!/bin/bash
SYSTEMD_SCRIPT_DIR=$( cd  $(dirname "${BASH_SOURCE:=$0}") && pwd)
cp -f "$SYSTEMD_SCRIPT_DIR/dspt.service" /etc/systemd/system
chown root:root /etc/systemd/system/dspt.service

systemctl daemon-reload
systemctl enable dspt.service
[Unit]
Description=My super service
After=multi-user.target network.target

[Service]
User=myuser
Group=mygroup
EnvironmentFile=/etc/environment
Type=idle
Restart=always
RestartSec=3
ExecStart=/home/myuser/miniconda3/envs/dspt/bin/run_dspt

[Install]
WantedBy=multi-user.target