为Python创建独立环境 [英] Create standalone environment for Python

查看:179
本文介绍了为Python创建独立环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发基于Python的工具,我需要将该工具分发给没有技术技能的用户.我分发该工具的方法是创建一个具有所有要求的安装程序,包括python可执行文件和库.

I'm developing a tool based on Python, and I need to distribute this tool for users without technical skills. My way to distribute the tool was to create an installer with all the requirements, including the python executable and the libraries.

为此,我尝试创建一个虚拟环境(使用pipenv创建它),并用我的代码复制了生成的工作目录.在某些情况下,安装程序可以运行是因为某些用户的计算机上安装了Python.对于没有Python的用户,需要一些依赖关系,但是依赖关系来自完整的python安装.

For this purpose, I tried to make a virtual environment (used pipenv to create it) and copied the resultant work directory with my code. In some cases the installer worked because some user has Python on it's machine; for user without Python some dependencies are required, but the dependencies are from a full python installation.

我已经尝试使用Cx_freeze,但是它不适用于我的应用程序.有没有一种方法可以使独立的python环境随我的应用程序一起分发?

I already tried to use Cx_freeze, but it doesn't work for my application. Is there a way to make a standalone python environment to distribute with my application?

推荐答案

我的解决方案是安装python版本和所需的软件包.我的解决方案结构是:

My solution was install a version of python and the needed packages. My solution structure was:


-> Python
-> Program
-> run.bat

第一个 Python 目录是包含所有可执行文件和程序包的python环境.目录 Program 包含项目文件和 run.bat ,它们将环境设置为可与此本地安装一起使用.目录Python\Scripts内有一个激活环境的档案.我的脚本代码是:

The first Python, directory is the python environment with all executables and packages. The directory Program contain the project files and the run.bat that set the environment to work with this local installation. Inside of directory Python\Scripts there is an archive to activate the environment. My scripts code are:

run.bat

cls
@ECHO OFF
call %~dp0Python\Scripts\activate.bat
ECHO Starting Program....
%~dp0Python\python %~dp0ProgramData\starter.py

activate.bat

@echo off

set "VIRTUAL_ENV=%~dp0\Python"

if defined _OLD_VIRTUAL_PROMPT (
    set "PROMPT=%_OLD_VIRTUAL_PROMPT%"
) else (
    if not defined PROMPT (
        set "PROMPT=$P$G"
    )
    if not defined VIRTUAL_ENV_DISABLE_PROMPT (
        set "_OLD_VIRTUAL_PROMPT=%PROMPT%"
    )
)
if not defined VIRTUAL_ENV_DISABLE_PROMPT (
    set "PROMPT=(64bits) %PROMPT%"
)

REM Don't use () to avoid problems with them in %PATH%
if defined _OLD_VIRTUAL_PYTHONHOME goto ENDIFVHOME
    set "_OLD_VIRTUAL_PYTHONHOME=%PYTHONHOME%"
:ENDIFVHOME

set PYTHONHOME=

REM if defined _OLD_VIRTUAL_PATH (
if not defined _OLD_VIRTUAL_PATH goto ENDIFVPATH1
    set "PATH=%_OLD_VIRTUAL_PATH%"
:ENDIFVPATH1
REM ) else (
if defined _OLD_VIRTUAL_PATH goto ENDIFVPATH2
    set "_OLD_VIRTUAL_PATH=%PATH%"
:ENDIFVPATH2

set "PATH=%VIRTUAL_ENV%;%PATH%"

此解决方案适用于我的两个应用程序(64位和32位)安装.

This solution works for in my both application (64 bits and 32 bits) installation.

这篇关于为Python创建独立环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆