首页 > 解决方案 > 如何在 Visual Studio 上运行 python 脚本和包

问题描述

我有一个 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 的模块是什么意思。我认为 IronPy 中存在此功能。

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

感谢您的帮助和支持,

蟒蛇脚本:呼吸_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()

视觉工作室:

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

标签: ironpython

解决方案


推荐阅读