带有 WINSW 的虚拟 Python 环境

问题描述 投票:0回答:1

我正在尝试创建一个使用虚拟 Python 环境的 Windows 服务。我使用 venv 创建了here 所描述的环境。为了创建 Windows 服务,我使用 winsw。 虽然我可以在虚拟环境中运行程序并设置 Windows 服务,但我不知道如何同时执行这两项操作。

我的第一次尝试是在Windows服务使用的.bat文件中调用“call win_env/Scripts/activate.bat”。这不起作用,因为他仍然使用主环境,因此忽略了这行代码。另外,这个issue中的第二个答案也提出了一个解决方案。这对我来说也不起作用,因为该程序仍然使用主要环境。 可能这是因为我没有在代码中使用环境变量,但我无法弄清楚如何从那里启动虚拟环境。

当我写它使用“主环境”时,我跟踪这一点,因为程序由于缺少某些依赖项(错误版本)而失败。这些依赖项安装在虚拟环境中,但不安装在主环境中。如果我在不进入虚拟环境的情况下调用 bat 文件,程序将失败并显示相同的消息。

错误信息:

Exception in thread {X}:
Traceback (most recent call last):
  File "{Path}\scoop\apps\anaconda3\current\lib\threading.py", line 973, in _bootstrap_inner
    self.run()
  File "{Path}\scoop\apps\anaconda3\current\lib\threading.py", line 910, in run
    self._target(*self._args, **self._kwargs)
  File "{Path}\Sources\{Project}\.\populate_cache.py", line 25, in query_failures_from_db
    df = pd.read_sql(statement, con, params=[date_from, date_to])
  File "{Path}\Sources\{Project}\win_env\lib\site-packages\pandas\io\sql.py", line 563, in read_sql
    pandas_sql = pandasSQL_builder(con)
  File "{Path}\Sources\{Project}\win_env\lib\site-packages\pandas\io\sql.py", line 744, in pandasSQL_builder
    import sqlite3
  File "{Path}\scoop\apps\anaconda3\current\lib\sqlite3\__init__.py", line 23, in <module>
    from sqlite3.dbapi2 import *
  File "{Path}\scoop\apps\anaconda3\current\lib\sqlite3\dbapi2.py", line 27, in <module>
    from _sqlite3 import *
ImportError: DLL load failed while importing _sqlite3: The specified module could not be found.

.bat 文件:

call uvicorn main:app --port 8590 --host 0.0.0.0

winsw 文件:

<?xml version="1.0"?>
<!--This configuration file should be placed near the WinSW executable, the name should be the same.E.g. for myapp.exe the configuration file name should be myapp.xmlYou can find more information about configuration options here: https://github.com/kohsuke/winsw/blob/master/doc/xmlConfigFile.md -->
<configuration>
<id>Test Programm</id>
<name>Test Programm</name>
<description> XXX. </description>
<executable>{PathToBatfile}</executable>
<onfailure delay="10 sec" action="restart"/>
<onfailure delay="20 sec" action="restart"/>
<onfailure action="none"/>
<resetfailure>1 hour</resetfailure>
<workingdirectory>{PathToWorkingdirectory}</workingdirectory> // .bat file is located here
<priority>Normal</priority>
<stoptimeout>15 sec</stoptimeout>
<stopparentprocessfirst>true</stopparentprocessfirst>
<startmode>Automatic</startmode>
<waithint>15 sec</waithint>
<sleeptime>1 sec</sleeptime>
<logpath>{PathToLog}</logpath>
<!--OPTION: logDefines logging mode for logs produced by the executable.Supported modes:* append - Rust update the existing log* none - Do not save executable logs to the disk* reset - Wipe the log files on startup* roll - Rotate logs based on size* roll-by-time - Rotate logs based on time* rotate - Rotate logs based on size, (8 logs, 10MB each). This mode is deprecated, use "roll"Default mode: appendEach mode has different settings.See https://github.com/kohsuke/winsw/blob/master/doc/loggingAndErrorReporting.md for more details -->
<log mode="roll"> </log>
</configuration>
python winsw
1个回答
1
投票

根据您提供的输入,我认为以下批处理文件应该有效:

call %~dp0win_env/Scripts/activate.bat
uvicorn main:app --port 8590 --host 0.0.0.0

%~dp0
解析为存储批处理文件的目录,因此它的工作独立于当前工作目录。)

请注意,

activate.bat
没有什么特别的作用。它只是更新您的
PATH
环境变量并更改命令提示符,这在开发过程中很方便。但是,您也可以只从虚拟环境中调用 Python 可执行文件,而不“激活”它。我对 uvicorn 不熟悉,但似乎它也可以被称为 Python 模块。在这种情况下,您还可以使用以下批处理文件:

win_env/Scripts/python.exe -m uvicorn main:app --port 8590 --host 0.0.0.0
© www.soinside.com 2019 - 2024. All rights reserved.