简体   繁体   English

如何激活Python虚拟环境并同时执行脚本?

[英]How to activate a Python virtual environment and execute a script at the same time?

I am trying to load automatically my Python virtual environment and execute a python query at the same time in Powershell but I am not getting any luck at it. 我试图在Powershell中自动加载我的Python虚拟环境并同时执行python查询,但是我对此没有任何运气。 This is the code I have: 这是我的代码:

# my_testing_file.py

# Activate the virtual environment
import os
script_directory = os.path.dirname(os.path.abspath(__file__))
activate_this_file = os.path.join(script_directory, 'C:\Temp\Development\Python_scripts\env\Scripts\activate_this.py')

# Start executing the main code
import pypyodbc
from openpyxl.workbook import Workbook

def main():
    cursor = initiate_connection_db()
    unposted_hours_results = retrieve_results_query(cursor)
    ...

if __name__ == "__main__":
    main()

All the code is in 1 single file and basically I want to do in Powershell python my_testing_file.py so it loads the virtual environment and executes the rest of the code. 所有代码都在一个文件中,基本上我想在Powershell python my_testing_file.py执行此操作,以便它加载虚拟环境并执行其余代码。

When this code gets executed from Powershell, a command prompt appears for a few seconds and then it shuts down and the rest of the code never gets executed. 当从Powershell执行此代码时,命令提示符将显示几秒钟,然后关闭,其余代码将永远不会执行。 Any help for fixing this code would be greatly appreciated. 修复此代码的任何帮助将不胜感激。

As @dfundako indicated, a post-activate script could have done the trick to activate the virtual environment and execute the script all at once, however, I found another alternative which I believe it is not the best but it performed what I needed to achieve. 正如@dfundako所指出的,激活后的脚本可以完成激活虚拟环境并立即执行脚本的全部操作,但是,我发现了另一个替代方法,我认为这不是最好的选择,但它可以满足我的要求。 I had to pack the python script and its libraries in an .exe file so once a user clicks on it, the program executes the desired task. 我必须将python脚本及其库打包到一个.exe文件中,因此,一旦用户单击它,该程序就会执行所需的任务。

Inside of my virtual environment, I executed this file called setup.py to build my_testing_file.py to an .exe: 在我的虚拟环境中,我执行了名为setup.py的文件以将my_testing_file.py生成为.exe:

# Python 3.6.7
# The cx_Freeze library did the trick to pack all the libraries and the script. 
# Put down all the scripts that your 

import sys, os
import openpyxl
import cx_Freeze
import my_testing_file.py
from subprocess import call

base = None

if sys.platform == 'win32':
    base = "Win32GUI"

# my_testing_file is the main script that executes my desired task.
executables = [cx_Freeze.Executable("my_testing_file.py", base=base)]

cx_Freeze.setup(
    name = "Testing Deploy",
options = {"build_exe":
           {"packages":
            ["openpyxl", "subprocess"]
            }
           },
version = "0.01",
description = " Testing executable alternative",
executables = executables
)

I believe something similar could have been done with Docker as well but I lack the required knowledge to achieve the same goal. 我相信使用Docker也可以完成类似的操作,但是我缺乏实现相同目标所需的知识。 Feel free to improve this answer or put the code for the Docker alternative or the aforementioned post-activate script. 随时改进此答案,或将代码用于Docker替代方案或上述激活后脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM