如何在Visual Studio上运行python脚本和包

时间:2019-05-15 18:04:28

标签: ironpython

我有一个Python程序来控制LIFX智能灯泡。我需要python代码通过Iron Python在Visual Studio上运行。 python程序位于GitHub上:

https://github.com/mclarkk/lifxlan

程序有两个目录:

  1. 示例:它包含用于控制Lifx灯泡闪电的python脚本。
  2. lifxlan:这是python脚本使用的软件包。

到目前为止,我已经能够下载Visual Studio中存在的Iron Python包。我可以运行一个简单的python打印脚本,例如print(“ hello world”)。这是我遇到的两个问题:

  1. 当我运行breath_all.py文件(在示例目录中)时,出现错误:

    没有名为copy的模块

我不知道没有名为copy的模块意味着什么。我以为IronPy中存在此功能。

  1. 我如何确保IronPy支持lifxlan软件包。到目前为止,我只是将整个python程序复制并粘贴到Visual Studio项目文件夹中。我不知道这是否合适。

感谢您的帮助和支持,

python脚本:breath_all

#!/usr/bin/env python
# coding=utf-8
import sys
from copy import copy
from time import sleep, time

from lifxlan import LifxLAN


def main():
    num_lights = None
    if len(sys.argv) != 2:
    print("\nDiscovery will go much faster if you provide the number of lights on your LAN:")
    print("  python {} <number of lights on LAN>\n".format(sys.argv[0]))
else:
    num_lights = int(sys.argv[1])

# instantiate LifxLAN client, num_lights may be None (unknown).
# In fact, you don't need to provide LifxLAN with the number of bulbs at all.
# lifx = LifxLAN() works just as well. Knowing the number of bulbs in advance
# simply makes initial bulb discovery faster.
lifx = LifxLAN(num_lights) #problem may occur in loading LifxLAN

# test power control
print("Discovering lights...")
original_powers = lifx.get_power_all_lights()
original_colors = lifx.get_color_all_lights()

half_period_ms = 2500
duration_mins = 20
duration_secs = duration_mins*60
print("Breathing...")
try:
    start_time = time()
    while True:
        for bulb in original_colors:
            color = original_colors[bulb]
            dim = list(copy(color))
            dim[2] = 1900
            bulb.set_color(dim, half_period_ms, rapid=True)
        sleep(half_period_ms/1000.0)
        for bulb in original_colors:
            color = original_colors[bulb]
            bulb.set_color(color, half_period_ms, rapid=True)
        sleep(half_period_ms/1000.0)
        if time() - start_time > duration_secs:
            raise KeyboardInterrupt
except KeyboardInterrupt:
    print("Restoring original color to all lights...")
    for light in original_colors:
        color = original_colors[light]
        light.set_color(color)

    print("Restoring original power to all lights...")
    for light in original_powers:
        power = original_powers[light]
        light.set_power(power)

if __name__=="__main__":
     main()

visual Studio:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;

namespace Test01
{
    class Program
     {
    static void Main(string[] args)
    {


        //ipy is the python interpreter
            var ipy = Python.CreateRuntime();
        dynamic test = ipy.UseFile("C:\\Users\\..\\source\\repos\\Test01\\Test01\\example\\breathe_all.py");
            test.main();

    }
}

}

错误:

引发异常:

'IronPython.Runtime.Exceptions.ImportException' in 
 Microsoft.Dynamic.dll
 An unhandled exception of type 
 'IronPython.Runtime.Exceptions.ImportException' occurred in 
 Microsoft.Dynamic.dll
 No module named copy

0 个答案:

没有答案