如何以编程方式/通过 Python 脚本安装 Python 模块? [英] How can I install Python modules programmatically / through a Python script?

查看:19
本文介绍了如何以编程方式/通过 Python 脚本安装 Python 模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以完全在脚本中从 PyPi 下载和安装 Python 模块,完全不使用外壳吗?

Can I download and install Python modules from PyPi strictly inside a script, without using a shell at all?

我使用非标准 Python 环境,即 Autodesk Maya 的 Python 解释器.这不带有easy_install",也没有shell",只有一个由主要 Maya 可执行文件调用的 python 脚本解释器.将 ez_setup.py 的内容复制并粘贴到脚本编辑器窗口并正确运行它会在 Maya 目录的某处安装一个 easy_install,但脚本错误地将 Python 解释器记录为...maya.exe"而不是...mayapy.exe"" 此外,使用easy_install需要一个shell.

I use a non-standard Python environment, Autodesk Maya's Python interpreter. This does not come with "easy_install," and there is no "shell," only a python script interpreter invoked by the main Maya executable. Copying and pasting ez_setup.py's contents into the script editor window and running it correctly installs an easy_install somewhere into Maya's directory, but the script incorrectly records the Python interpreter as "...maya.exe" instead of "...mayapy.exe" Furthermore, using easy_install requires a shell.

目标是提供一个 Python 脚本,例如,将 NumPy 安装到 Maya Python 系统中.这可以通过将鸡蛋放入 site-packages 目录来完成,但这需要用户手动干预.最终用户在 Maya 环境之外所做的任何事情本质上都是不可触碰的,尤其是与文件系统有关的事情.但是通过脚本搞乱文件系统?没关系.

The objective is to deliver a Python script that, e.g., installs NumPy into the Maya Python system. This could be accomplished by dropping eggs into the site-packages directory, but that requires manual user intervention. Anything an end user has to do outside the Maya environment is essentially untouchable, especially messing with the file system. But messing with the filesystem through a script? That's fine.

有比 ez_setup.py + 编辑生成的 easy_install...py 的 + 子进程调用更优雅的东西吗?我觉得这是一个基本功能.我在网上看到关于通过pip安装程序化模块的文档...但是需要先安装pip!

Is there something more elegant than ez_setup.py + editing the resulting easy_install...py's + subprocess calls? I feel like this is a basic feature. I see documentation online for programmatic module installation through pip... but pip needs to be installed first!

严格在脚本范围内安装模块的最优雅的方法是什么?

What is the most elegant way to install a module strictly within the confines of a script?

推荐答案

在 Windows 上为 Maya 安装 easy_install.

Installing easy_install for Maya on windows.

  1. 下载ez_setup.py.
  2. 打开windows cmd提升(开始,输入cmdrmb点击它->以管理员身份运行)
  3. 将cmd目录改为x:maya install dirin
    • 示例:cd c:Program FilesMayaXXin
  1. Download ez_setup.py.
  2. open windows cmd elevated (start, type cmd, rmb click on it ->run as administrator)
  3. change the cmd directory to x:maya install dirin
    • example: cd c:Program FilesMayaXXin

现在应该正确设置easy install.您可能仍希望执行以下步骤:

Now easy install should be set up properly. You may want to still do following steps:

  1. cd x:maya install dirpythonscripts
  2. 重命名此文件夹中的所有文件以 ma 开头
    • 例子:for %i in (*) do ren %i ma%i
  • 点击 win+e
  • rmb我的电脑并选择属性
  • 高级系统设置 -> 环境变量
  • 搜索变量路径编辑它并附加;x:maya install dirpythonscripts

现在您可以从 cmd 调用 ma​​easy_install pythonModule 来安装东西.您也可以在 Maya 中调用以下方法来安装模块:

Now you can call maeasy_install pythonModule from cmd for installing stuff. Also you can call following inside Maya to install modules:

from setuptools.command import easy_install
easy_install.main( ["pythonModule"] )

注意:如果 Maya 安装在程序文件中,那么如果不升级,您就无法真正安装东西.除非您更改 Maya python 目录的磁盘权限.

NOTE: If Maya is installed in program files then you can not really install stuff without elevating. Unless you change disk permissions to the Maya python directory.

这篇关于如何以编程方式/通过 Python 脚本安装 Python 模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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