# 项目设置

## 项目设置

这篇文章讨论如何向 Python 程序中引入 Python 库。

### 库

LeapMotion 的 Python API 作为 Python 扩展模块提供了一个包含 Python本身及本地代码。Python API 支持 Python 2.7。

|          | Windows                                           | Mac                                           | Linux                                              |
| -------- | ------------------------------------------------- | --------------------------------------------- | -------------------------------------------------- |
| Python模块 | lib/Leap.py                                       | lib/Leap.py                                   | lib/Leap.py                                        |
| 32位本地库   | <p>lib/x86/LeapPython.pyd<br>lib/x86/Leap.dll</p> |                                               | <p>lib/x86/LeapPython.so<br>lib/x86/libLeap.so</p> |
| 64位本地库   | <p>lib/x64/LeapPython.pyd<br>lib/x64/Leap.dll</p> |                                               | <p>lib/x64/LeapPython.so<br>lib/x64/libLeap.so</p> |
| 通用二进制本地库 |                                                   | <p>lib/LeapPython.so<br>lib/libLeap.dylib</p> |                                                    |

你可以在下载的 LeapMotion SDK 包的`lib`文件夹中找到这些库文件。在 WIndows 和 Linux 上，32位 Python 需要使用 x86的库文件，64位的 Python 需要使用 x64 的库文件。

LeapMotion 没有把库作为一个标准的 Python 包直接安装到用户电脑上。而是把库作为你应用的一个内置模块，你应该吧库和你的应用程序一起发布。

### 包含 LeapMotion 模块

为了引入 Leap 模块，这些苦文件必须放置到和 Python 运行时可以找到的地方。最简单的方法就是把库文件和程序代码放到同一个目录中。Python 会在同一个目录里导入这个模块。

如果你想把库放在与你源码不同的目录，你可以在引入模块前把库文件的路径添加到`sys.path`中：

```python
import sys
sys.path.insert(0, "path/to/leap/libraries")
import Leap
```

例如，你项目的文件结构可以像这样：

```
SnakesAndLadders/
    src/
        Snakes.py
        Ladders.py
    lib/
```

你也可以将 LeapMotion 库文件拷贝到`lib`子文件夹中（确保复制的是适合你的平台架构的文件）。比如在 Mac 上，你的项目结构可以这样：

```
SnakesAndLadders/
    src/
        Snakes.py
        Ladders.py
    lib/
        Leap.py
        LeapPython.so
        libLeap.dylib
```

于是你可以使用下面的方法在`Snakes.py`或者`Ladders.py`中引入 Leap 模块：

```python
import sys
sys.path.insert(0, "../lib")
import Leap
```

这种引入的方式假设你的 Python 文件总是运行在当前工作目录下运行。为使得主程序能够从任何位置运行时都能正常工作，你可以使用 Python`inspect`模块获取包含源文件的路径并设置`sys.path`关联到那个文件件：

```python
import os, sys, inspect
src_dir = os.path.dirname(inspect.getfile(inspect.currentframe()))
lib_dir = os.path.abspath(os.path.join(src_dir, '../lib'))
sys.path.insert(0, lib_dir)
import Leap
```

**注意**：如果你开发的程序只为你自己使用，你可以把 Leap 模块和其他需要的库放在任何位置，比如其中一个文件夹放置`sys.path`变量，另一个文件夹是你的`PYTHONPATH`环境变量等等。只需在更新 SDK 的时候你还能记得你把它们放到哪儿了。然而，自从 Leap 模块目前不在 Python 包管理器有效，不推荐吧 LeapMotion 拷贝到 Python 标准模块索引的位置。这样做会导致多个程序尝试安装他们不同的 LeapMotion 库时而发生冲突。

### 支持 32 位和 64 位 Python 架构

要同时支持 Windows 和 Linux 32位及64位架构，你可以运行时检查然后用 `sys.path` 设置正确的文件即可。在 Mac 中，LeapMotion 库是通用二进制的，支持两种架构，这种技术是不需要的。

以 SnakeAndLadders 为例，在程序里你应该吧两种库都分别放到两个文件夹中，例如：

```
SnakesAndLadders/
    src/
        Snakes.py
        Ladders.py
    lib/
        x86/
            Leap.py
            LeapPython.so
            libLeap.so
        x64/
            Leap.py
            LeapPython.so
            libLeap.so
```

然后引入合适的 Leap 模块：

```python
import os, sys, inspect
src_dir = os.path.dirname(inspect.getfile(inspect.currentframe()))
arch_dir = '../lib/x64' if sys.maxsize > 2**32 else '../lib/x86'
sys.path.insert(0, os.path.abspath(os.path.join(src_dir, arch_dir)))

import Leap
```

相同的办法你可以同时支持更多平台。

### 使用不同的 Python 发行版

在 Mac、Linux、或者自荐的 Python 中使用 LeapMotion 的 Python 库，你必须更新 `LeapPython.so` 的加载路径到 Python 的相应实例。

首先，运行`otool`工具来显示当前的加载路径：

```bash
otool -L LeapPython.so
```

显示结果与下面很相似：

```
LeapPython.so:
    @loader_path/LeapPython.so (compatibility version 0.0.0, current version 0.0.0)
    /Library/Frameworks/Python.framework/Versions/2.7/Python (compatibility version 2.7.0, current version 2.7.0)
    @loader_path/libLeap.dylib (compatibility version 0.7.0, current version 2.0.1)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)
    /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 56.0.0)
```

Python位于 `/Library/Frameworks/Python.framework/Versions/2.7/Python` 这行，它需要用`install_name_tool`工具来修改。

其次，使用 `install_name_tool` 工具把 Python 的路径修改到你所需要的位置，例如：&#x20;

```python
install_name_tool -change /Library/Frameworks/Python.framework/Versions/2.7/Python \
/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib \
LeapPython.so
```

**注意**：`otool`和`install_name_tool`都是标准的 Linux 和 OS X 命令行工具。

## 为 Python 3 重新编译 LeapPython

LeapMotion SDK 包含的 LeapPython 库文件只支持 Python2.7。然而 SDK 还包含 SWIG 结构文件，它可以用来生成 LeapPython 源代码。所以，高级用户可以生成并编译他们自己的 LeapPython。作为指导，请参考[使用 SWIG2.0.9 生成 Python3.3.0 包](https://support.leapmotion.com/entries/39433657-Generating-a-Python-3-3-0-Wrapper-with-SWIG-2-0-9)。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://changkun.gitbook.io/leapmotion_v2_cn/yu-yan/cpp/ying-yong-cheng-xu-kai-fa/project_setup.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
