简体   繁体   English

Mac OS自带的Python环境

[英]Self-contained Python environment for Mac OS

I need to make a fully self-contained Python environment for Mac, which can be transferred between different Macs running OSX and function independently of any other Python installations (or lack thereof).我需要为 Mac 制作一个完全独立的 Python 环境,它可以在运行 OSX 和 function 的不同 Mac 之间传输,独立于任何其他 ZA7F5F35426B927411FC9231B563827 安装(或缺少)。

In the enviroments I have created (using venv ), I find that the /bin/python file and others are in fact aliases.在我创建的环境中(使用venv ),我发现 /bin/python 文件和其他文件实际上是别名。 When I replace these aliases with the original executables, the enviroment still does not work on other computers due references to files within a Python installation, such as当我用原始可执行文件替换这些别名时,由于引用了 Python 安装中的文件,该环境仍然无法在其他计算机上运行,例如

Library not loaded: /Library/Frameworks/Python.framework/Versions/3.9/Python

Is there a way to make the environment fully self-contained?有没有办法让环境完全自给自足?

I would guess so, since tools such as pyinstaller and cx_freeze exist to make executables out of Python applications.我猜是这样,因为存在诸如pyinstallercx_freeze类的工具可以从 Python 应用程序中生成可执行文件。 So these must contain standalone environments somehow.所以这些必须以某种方式包含独立的环境。 But for my purposes, it is the enviroment that I need.但就我的目的而言,这是我需要的环境。 Not an executable as these tools provide.不是这些工具提供的可执行文件。

You can try using a virtual environment.您可以尝试使用虚拟环境。 Python comes with venv which allows you to create virtual environments. Python 带有venv ,允许您创建虚拟环境。 You could consider them self-contained, since all the necessary scripts for running Python are in the virtual environment.您可以认为它们是独立的,因为运行 Python 所需的所有脚本都虚拟环境中。 This goes with the package dependencies as well.这也适用于 package 依赖项。 You can have a variety of packages installed on your computer, but the virtual environment will have none of them installed (unless, of course, you install them yourself).您可以在您的计算机上安装各种软件包,但虚拟环境不会安装它们(当然,除非您自己安装它们)。

To create a virtual environment, run:要创建虚拟环境,请运行:

$ py -3.9 -m venv virtualenv

You can replace 3.9 with whichever Python version you are using and you can replace virtualenv with whatever you want to name your virtual environment.您可以将3.9替换为您正在使用的 Python 版本,您可以将virtualenv替换为您想要命名虚拟环境的任何内容。

To activate the virtual environment, you would run:要激活虚拟环境,您将运行:

$ source virtualenv/Scripts/activate

And to deactivate it, you would run:要停用它,您将运行:

$ deactivate

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

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