我正在尝试创建一个使用虚拟Python环境的Windows服务。我创建了使用venv描述的这里环境。要创建Windows,我使用温索。虽然我既可以在虚拟环境中运行一个程序,也可以设置一个windows服务,但我无法同时解决这两个问题。
我的第一次尝试是在windows服务使用的.bat文件中调用"call win_env/Script/activate.bat“。这是行不通的,因为他仍然使用主要环境,因此忽略了这一行代码。另外,这个问题中的第二个答案提出了一个解决方案。这也不适用于我,因为程序仍然使用的主要环境。这可能是因为我没有在代码中使用env变量,但我不知道如何从那里启动虚拟env。
当我写到它使用“主环境”时,我跟踪它,因为程序因为缺少一些依赖项(错误的版本)而失败。这些依赖项安装在虚拟环境中,而不是在主环境中。如果我在没有进入虚拟环境的情况下调用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.0winsw文件:
<?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>发布于 2022-03-14 12:52:04
根据您提供的输入,我认为以下批处理文件应该可以工作:
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.0https://stackoverflow.com/questions/71466763
复制相似问题