简体   繁体   English

如何在Windows的Anaconda环境中部署python程序?

[英]How to deploy python programs in Anaconda environments on Windows?

I am trying to use Anaconda and conda environments to allow Python programs for data acquisition* etc. to run from the (Anaconda) command line on Windows. 我正在尝试使用Anaconda和conda环境来允许Python程序进行数据采集*等,以便从Windows上的(Anaconda)命令行运行。 The set up will be that the Python programs are installed to a particular location (cloned from Github), within %PATH% or whichever environment variable is more appropriate. 设置是将Python程序安装在%PATH%或更合适的环境变量内的特定位置(从Github克隆)。

From an Anaconda command prompt in another directory and a particular conda environment, I want (both myself and other users) to be able run either python test.py <args> or test.py <args> (either solution is acceptable) and have a system wide conda environment run its Python to execute the program. 从另一个目录和特定conda环境中的Anaconda命令提示符下,我希望(本人和其他用户)能够运行python test.py <args>test.py <args> (两种解决方案均可接受)并具有系统范围的conda环境运行其Python执行该程序。 test.py can/will have an appropriate shebang set. test.py可以/将设置适当的shebang设置。

Right now the python test.py calls the correct Python within the active conda environment, but cannot find the test.py program as Python won't search the %PATH% or similar looking for the program. 现在, python test.py在活动的conda环境中调用了正确的Python,但是找不到test.py程序,因为Python不会搜索%PATH%或类似的程序。 test.py does something (Windows does not complain that the executable can't be found, and I've been playing with the file associations to get this far), but doesn't appear to start Python - a simple print function or raise statement as the only entry in the file does nothing. test.py执行某些操作(Windows不会抱怨找不到可执行文件,并且我一直在与文件关联进行深入研究),但是似乎没有启动Python-一个简单的打印功能或提高语句作为文件中的唯一条目不会执行任何操作。

I've tried setting file associations in Windows, but this hasn't changed anything. 我曾尝试在Windows中设置文件关联,但这并没有改变任何内容。 I've copied the py.exe/pyw.exe across to the Anaconda environments, with no change. 我已经将py.exe / pyw.exe复制到了Anaconda环境中,没有任何更改。

Is this something that can be done within Anaconda, or am I going to have to fall back on installing base Python directly and trying to use the launcher mechanism there? 这是可以在Anaconda中完成的事情,还是我不得不不得不直接安装基本的Python并尝试在其中使用启动器机制?

Note that I'm also intending to deploy these programs on Raspbian, so any solutions, including non-Anaconda ones, that will work cross platform there would be worth extra effort on my part. 请注意,我也打算将这些程序部署在Raspbian上,因此任何可以跨平台使用的解决方案,包括非Anaconda的解决方案,都值得我付出更多的精力。

*these programs have significant usage of library packages for accessing external USB/GPIB/serial/ethernet connected lab equipment and use matplotlib, scipy, etc., hence the desire for a cloneable conda environment as the base environment. *这些程序在访问外部USB / GPIB /串行/以太网连接的实验室设备以及使用matplotlib,scipy等方面具有大量的库包用法,因此需要可克隆的conda环境作为基本环境。

It turns out the correct answer to this is fairly simple, but is fairly hard to find explained well. 事实证明,正确的答案很简单,但是很难找到很好的解释。 This might be a little clearer than the other answers I found: 这可能比我发现的其他答案更清楚:

Install the standalone launcher from pylauncher and add #!/usr/bin/env python shebangs to your scripts. pylauncher安装独立的启动器,然后将#!/usr/bin/env python shebangs添加到脚本中。

This should register .py files to Python.File and will find your Anaconda Pythons in appropriate environments. 这应该将.py文件注册到Python.File并将在适当的环境中找到您的Anaconda Python。 If you don't have a non-Anaconda python, it will use the Anaconda base environment (these two facts were the key element I was missing from various other answers around this problem that I had looked at, including the documents on python.org ). 如果您没有非Anaconda python,它将使用Anaconda基本环境(这两个事实是我针对此问题而在各种其他答案中缺少的关键要素,包括python.org上的文档)。

If you have a Python from python.org installed, then a standalone command line shell will use that, defaulting to Python 2.x, then Python 3.x. 如果您安装了python.org的Python,则将使用独立的命令行外壳,默认情况下为Python 2.x,然后为python.org With #!/usr/bin/env python shebang, then a regular command shell will try to use python.org pythons first, then the Anaconda base environment. 使用#!/usr/bin/env python shebang,然后常规命令外壳将首先尝试使用python.org #!/usr/bin/env python ,然后使用Anaconda基本环境。 An Anaconda prompt will use the active environment. Anaconda提示将使用活动环境。 #! /usr/bin/env python2 #! /usr/bin/env python2 or python3 will try to use python.org pythons only and fail if they are not found. #! /usr/bin/env python2python3将仅尝试使用python.org ,如果找不到,则将失败。

Installing Python 2.7 from python.org installers (and letting the installer set the file associations) will break pylauncher, and reinstalling will not fix it. python.org安装程序安装Python 2.7(并让安装程序设置文件关联)将破坏pylauncher,并且重新安装不会对其进行修复。 Instead, set Computer\\HKEY_CLASSES_ROOT\\Python.File\\Shell\\open\\command default value to "C:\\WINDOWS\\py.exe" "%L" %* to revert back to the pylauncher set up (assuming you used the launchwin.* packages to install it). 而是将Computer\\HKEY_CLASSES_ROOT\\Python.File\\Shell\\open\\command默认值设置为"C:\\WINDOWS\\py.exe" "%L" %*以恢复为pylauncher设置(假设您使用了launchwin.*要安装的软件包)。

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

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