簡體   English   中英

帶WINSW的虛擬Python環境

[英]Virtual Python enviroment with WINSW

我正在嘗試創建一個使用虛擬 Python 環境的 Windows 服務。 我使用 venv 創建了這里描述的環境。 要創建 Windows 服務,我使用winsw 雖然我既可以在虛擬環境中運行程序又可以設置 windows 服務,但我不知道如何同時執行這兩項操作。

我的第一次嘗試是在 windows 服務使用的 .bat 文件中調用“call win_env/Scripts/activate.bat”。 這沒有用,因為他仍然使用主環境,因此忽略了這行代碼。 問題的第二個答案也提出了解決方案。 這對我也不起作用,因為該程序仍然使用主要環境。 這可能是因為我沒有在代碼中使用環境變量,但我不知道如何從那里啟動虛擬環境。

當我寫到它使用“主環境”時,我會跟蹤它,因為程序由於缺少一些依賴項(錯誤的版本)而失敗。 這些依賴項安裝在虛擬環境中,但不安裝在主環境中。 如果我在不進入虛擬環境的情況下調用 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

文件:

<?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>

根據您提供的輸入,我認為以下批處理文件應該可以工作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM