简体   繁体   English

Windows中调用python脚本时自动打开虚拟环境

[英]Automatically open a virtual environment when calling a python script in Windows

This question has been asked before, but the answers are all several years old and I could not get any to work for me, so I would appreciate some help.这个问题以前曾被问过,但答案都是几年前的,我无法为我工作,所以我会很感激一些帮助。

The question is simple, really: I have a python script, and a virtual environment I want it to run in when I double-click it, or call it from another program.问题很简单,真的:我有一个 python 脚本和一个虚拟环境,当我双击它或从另一个程序调用它时,我希望它在其中运行。 How can I achieve this?我怎样才能做到这一点?

Some ideas一些想法

Shebang社邦

Place a shebang pointing to the Python interpreter inside the virtual environement with the full path at the top of the script.在虚拟环境中放置一个指向 Python 解释器的shebang ,完整路径位于脚本顶部。

#!/path/to/my/venv/bin/python

import sys

print(sys.executable)

Shell/batch script外壳/批处理脚本

Write a shell script wrapping the call to the actual Python script:编写一个 shell 脚本来包装对实际 Python 脚本的调用:

#!/usr/bin/env sh

/path/to/my/venv/bin/python /path/to/my/script.py

Python wrapper Python 包装

#!/usr/bin/env python3

import subprocess

command = [
    '/path/to/my/venv/bin/python',
    '/path/to/my/script.py',
]
subprocesss.check_call(command)

you must add packages directory address to your sys.path您必须将包目录地址添加到您的sys.path

sys.path a built-in variable within the sys module. sys.path sys 模块中的内置变量。 It contains a list of directories that the interpreter will search in for the required module.它包含解释器将在其中搜索所需模块的目录列表。

add line below to top of your script, now you can double click on script file and every thing must work fine在脚本顶部添加下面的行,现在您可以双击脚本文件,一切都必须正常工作

import sys; sys.path.append("./<environment path>/Lib/site-packages")

this is simple solution however, as sinoroc said it's not good solution because in this situation you are using system interpreter not virtual environment interpreter so i wouldn't be surprised if things don't work as expected.这是一个简单的解决方案,但是正如sinoroc所说,这不是一个好的解决方案,因为在这种情况下,您使用的是系统解释器而不是虚拟环境解释器,所以如果事情没有按预期工作,我不会感到惊讶。


also you can download all packages that you need and extract them into single folder and add that folder to sys.path你也可以下载你需要的所有包并将它们解压缩到单个文件夹中,然后将该文件夹添加到sys.path

first download packages you need with command below首先使用以下命令下载您需要的软件包

pip download <package names> --dest <directory name>

for example:例如:

pip download requests --dest packages

at the end add folder contain you packages to your path最后添加文件夹包含你的包到你的路径

import sys; sys.path.append("./<packages directory>")

Recommended Solution推荐解决方案

the best solution is that write little batch script for windows or shell script for linux that automatically active virtual environment and then run your script最好的解决方案是为 windows 或 shell 脚本编写小批处理脚本,为 linux 自动激活虚拟环境,然后运行您的脚本

first create file with .bat extention for windows or .sh extension for linux and then add line below to it首先为 windows 创建带有.bat扩展名的文件或为 linux 创建带有.sh扩展名的文件,然后在下面添加行

for .bat file对于.bat文件

<environment path>\Scripts\activate && python <script name>.py

for .sh file对于.sh文件

source <environment path>/bin/activate && python <script name>.py

now you can click on this batch script * .bat file or shell script * .sh file and everything work's fine现在您可以单击此批处理脚本 * .bat文件或 shell 脚本 * .sh文件,一切正常

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

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