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

查看:86
本文介绍了如何通过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的内容复制并粘贴到脚本编辑器窗口中并正确运行,会将easy_install安装到Maya的目录中,但是该脚本错误地将Python解释器记录为"... maya.exe"而不是"... mayapy.exe" 而且,使用easy_install需要外壳.

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的窗口(开始,键入 cmd rmb 单击它->以管理员身份运行)
  3. 将cmd目录更改为x:\ maya install dir \ bin
    • 示例:cd c:\ Program Files \ MayaXX \ bin
  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 dir\bin
    • example: cd c:\Program Files\MayaXX\bin

现在应该正确设置易于安装的了.您可能仍需要执行以下步骤:

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

  1. cd x:\ maya安装目录\ python \ scripts
  2. 重命名此文件夹中的所有文件以ma开头
    • 示例:对于(*)中的%i,请执行%i ma%i
  1. cd x:\maya install dir\python\scripts
  2. rename all files in this folder to start with ma
    • example: for %i in (*) do ren %i ma%i
  • 打Win + e
  • 在我的计算机上
  • rmb 并选择属性
  • 高级系统设置->环境变量
  • 搜索变量路径进行编辑并附加; x:\ maya install dir \ python \ scripts
  • hit win+e
  • rmb my computer and choose properties
  • Advanced system settings -> Environment variables
  • search variable path edit it and append ;x:\maya install dir\python\scripts

现在,您可以从cmd调用 maeasy_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天全站免登陆