C# 如何在 Windows 上为 .NET 安装 Python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14633695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:31:06  来源:igfitidea点击:

How to install Python for .NET on Windows

c#python.netpython.netpythonnet

提问by user441521

I downloaded Python for .NET.
Inside the zip is clr.pyd, nPython.exe, Python.Runtime.dlland 2 debug database files.
I put the clr.pyd and Python.Runtime.dll in my python DLLs dir C:\Python27\DLLs thinking this is all that's needed for installation. I then open up the Python GUI and type import clr and I get:

为 .NET下载了Python
里面的拉链是clr.pydnPython.exePython.Runtime.dll和2个调试数据库文件。
我将 clr.pyd 和 Python.Runtime.dll 放在我的 python DLLs 目录 C:\Python27\DLLs 中,认为这就是安装所需的全部内容。然后我打开 Python GUI 并输入 import clr ,我得到:

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    import clr
SystemError: dynamic module not initialized properly

New to python but not .NET and want to use the CPython and not IronPython. What am I missing in this installation? The readme of Python for .NETsays there is an installation for Windows package but all I found was the zip file.

刚接触 python 但不是 .NET 并且想要使用 CPython 而不是 IronPython。我在这个安装中缺少什么?Python for .NET的自述文件说有一个 Windows 包的安装,但我发现的只是 zip 文件。

回答by Devin

The correct way to install Python for .NET is to copy ALL the filesfrom the .zip into the directory that contains your .py file.

为 .NET 安装 Python 的正确方法是将 .zip 中的所有文件复制到包含 .py 文件的目录中。

Then if you execute

那么如果你执行

python yourfilename.py

you should find that your "import clr" statement works. I have tested this using python 2.7 x86 and pythonnet-2.0-Beta0-clr4.0_140_py27_UCS2_x86.zip

您应该会发现您的“import clr”语句有效。我已经使用 python 2.7 x86 和 pythonnet-2.0-Beta0-clr4.0_140_py27_UCS2_x86.zip 对此进行了测试

I was able to replicate your error by only copying the clr.pyd file into my working directory.

我只能通过将 clr.pyd 文件复制到我的工作目录中来复制您的错误。

Note that I was unable to make this work in Python 3.3 x86 or Python 3.3 x64

请注意,我无法在 Python 3.3 x86 或 Python 3.3 x64 中完成这项工作

回答by Jonno

I don't know why yet but the only way I could get it to work is to copy those 3 files into the C:\Python27\ directory directly.

我不知道为什么,但我可以让它工作的唯一方法是将这 3 个文件直接复制到 C:\Python27\ 目录中。

回答by denfromufa

The proper way to load CLR in Python is like this:

在 Python 中加载 CLR 的正确方法是这样的:

  1. Make sure no old stuff is left from Python.NET in Python installation folder (e.g. C:\Python27). In my case I had legacy clr.pydin one of folders. Note that pip for some old versions did not remove all parts of Python.NET.
  2. Append the directory with Python.NET files (clr.pydand Python.Runtime.dll) to sys.path
  1. 确保 Python.NET 在 Python 安装文件夹(例如 C:\Python27)中没有遗留旧的东西。就我而言,我clr.pyd在其中一个文件夹中有遗产。请注意,某些旧版本的 pip 并未删除 Python.NET 的所有部分。
  2. 将带有 Python.NET 文件(clr.pydPython.Runtime.dll)的目录附加到sys.path

Now you can load CLR the most flexible way without even installing to Python directories!

现在您可以以最灵活的方式加载 CLR,甚至无需安装到 Python 目录!

You can compile from source on github:

您可以从 github 上的源代码编译:

pip install git+https://github.com/pythonnet/pythonnet

or use Windows wheels/installers from Christoph Gohlke:

或使用 Christoph Gohlke 的 Windows 轮子/安装程序:

http://www.lfd.uci.edu/~gohlke/pythonlibs/#pythonnet

http://www.lfd.uci.edu/~gohlke/pythonlibs/#pythonnet

PyPI package is available for installation from pip:

PyPI 包可用于从 pip 安装:

https://pypi.python.org/pypi/pythonnet

https://pypi.python.org/pypi/pythonnet

More installation options such docker, WinPython, conda, nuget, choco are listed here:

更多安装选项如 docker、WinPython、conda、nuget、choco 在这里列出:

https://github.com/pythonnet/pythonnet/wiki/Installation

https://github.com/pythonnet/pythonnet/wiki/Installation

回答by bc3tech

I actually took matters in to my own hands here and created a Python.Net Chocolatey package. to install, simply run

我实际上在这里亲自处理并创建了一个 Python.Net Chocolatey 包。安装,只需运行

cinst pythonnet

蟒蛇网

at the command line. Maybe this will help users having issues getting this to run.

在命令行。也许这将帮助用户在运行时遇到问题。

回答by MSlimmer

If you are planning to freeze with py2exe or pyinstall be sure to install the dev version. There is something wrong with the 2.0.0 release when packaged with py2exe, pyinstaller and maybe other freezers. But 2.1.0.dev1 works well. So...

如果您打算使用 py2exe 或 pyinstall 冻结,请务必安装开发版本。与 py2exe、pyinstaller 和其他冷冻机一起打包时,2.0.0 版本有问题。但是 2.1.0.dev1 运行良好。所以...

pip install --pre pythonnet

pip install --pre pythonnet

And you'll have to add the pythone.runtime.dll to the bundle (see docs for your preferred bundler). There is also a problem with how py2exe loads the dll when using bundle_files: 1 (single exe).

并且您必须将 pythone.runtime.dll 添加到包中(请参阅您首选的打包器的文档)。py2exe在使用bundle_files时如何加载dll也存在问题:1(单个exe)。