HOUDINI TIP | USING HOU MODULE IN VISUAL STUDIO CODE_手动在vscode里设置houdiniPython模块

本文转载自:https://jurajtomori.wordpress.com/2018/02/20/houdini-tip-using-hou-module-in-visual-studio-code/,版权归原作者或者来源机构所有。转载请注明出处。

译文为GOOGLE机翻,作为参考

When developing Python tools for Houdini it is often more convenient to use an external IDE. However we do not want to miss Houdini’s neat auto-completion. In this quick post I will show how to set-up Visual Studio Code (VSCode) auto-completion for hou module.
在为Houdini开发Python工具时,通常更方便使用外部IDE。 但是,我们不想错过Houdini的自动完成功能。 在这篇快速文章中,我将展示如何为hou模块设置Visual Studio Code(VSCode)自动完成功能。
在这里插入图片描述
UPDATE 1: Added Interpreter settings and config examples for Windows and Linux.
UPDATE 2: Corrected the part about hython, added a better solution, which does not break autocompletion.
更新1:添加了Windows和Linux的解释器设置和配置示例。
更新2:更正了有关hython的部分,添加了更好的解决方案,该解决方案不会破坏自动完成功能。

The first step is to determine hou module’s location on disk. This can vary between various systems and you can check it from Houdini’s Python shell window by printing hou.file:
第一步是确定hou模块在磁盘上的位置。 不同的系统可能会有所不同,您可以通过打印hou .__ file__在Houdini的Python Shell窗口中进行检查:

>>> print hou.__file__
 /opt/hfs16.5.473/houdini/python2.7libs/hou.pyc

On Windows the path can look like this: C:\Program Files\Side Effects Software\Houdini 16.5.473\houdini\python2.7libs\hou.pyc .
Windows上,路径如下所示:C:\ Program Files \ Side Effects Software \ Houdini 16.5.473 \ houdini \ python2.7libs \ hou.pyc

This information is needed for locating Houdini Python modules. The next thing we need to do is to configure Python in VSCode to pick up those Houdini modules. You can set it globally in User Settings, or just for your project in Workspace Settings. Add the following lines and replace the path pointing to the modules.
查找Houdini Python模块需要此信息。 我们需要做的下一件事是在VSCode中配置Python以选择那些Houdini模块。 您可以在“用户设置”中全局设置它,也可以仅在“工作区设置”中为您的项目设置它。 添加以下行并替换指向模块的路径。

"python.autoComplete.extraPaths": [ "/opt/hfs16.5.473/houdini/python2.7libs" ],
"python.autoComplete.preloadModules": ["hou"]

The other setting should speed up loading of the hou module, which can take a bit of time.
Now you can press ctrl+space with your cursor above a Houdini class and you will see suggestions with the corresponding documentation.
其他设置应加快hou模块的加载速度,这可能需要一些时间。
现在,您可以在Houdini类上方按Ctrl +空格键,然后会看到带有相应文档的建议。

If you are also using Pylint in VSCode, you need to point Pylint to hou module as well. You can create .pylintrc file in your linux home directory with the following content:
如果您还在VSCode中使用Pylint,则还需要将Pylint指向hou模块。 您可以在linux主目录中使用以下内容创建.pylintrc文件:

[MASTER]
 init-hook='import sys; sys.path.append("/opt/hfs16.5.473/houdini/python2.7libs")'

Windows users can find how to create .pylintrc file here.
Since creating a file beginning with a dot is not allowed from Windows file explorer, you can create a template using:
Windows用户可以在此处找到如何创建.pylintrc文件。
由于Windows文件浏览器不允许创建以点开头的文件,因此可以使用以下方法创建模板:

pylint --generate-rcfile > .pylintrc

If you want to disable linting, you can use this setting:
如果要禁用,可以使用以下设置:

"python.linting.pylintEnabled": false

Also note that VSCode’s Python auto-completion will now take Houdini’s licence (Houdini Batch license).
另请注意,VSCode的Python自动完成功能现在将获得Houdini的许可证(Houdini Batch许可证)。

Now you should be able to see auto-completion suggestions for your code, but if you will try to run your scripts from VSCode, you will probably see this error message:
现在,您应该能够看到代码的自动完成建议,但是如果您尝试从VSCode运行脚本,则可能会看到以下错误消息:

Traceback (most recent call last):
 File "/your/scripts/folder/script.py", line 1, in <module>
 import hou
ImportError: No module named hou

(You can run your scripts from VSCode by Ctrl+Shift+P -> Run Python File in Terminal, or simply by selecting this command in right-click menu in the editor.)
(您可以通过Ctrl + Shift + P->在终端中运行Python文件从VSCode运行脚本,或者只需在编辑器的右键菜单中选择此命令即可。)

Originally I suggested setting hython as your Python interpreter in VSCode. This would enable you to run Python scripts directly from VSCode, but would eventually break auto-completion.
最初,我建议将hython设置为VSCode中的Python解释器。 这将使您能够直接从VSCode运行Python脚本,但最终会破坏自动完成功能。

A better solution is to specify environment variables for Integrated Terminal in VSCode, which will point system’s Python interpreter to Houdini modules.
更好的解决方案是在VSCode中为Integrated Terminal指定环境变量,这会将系统的Python解释器指向Houdini模块。

On Linux we need to specify PYTHONPATH environment variable, which Python uses for looking for modules. On Windows we need to specify PYTHONPATH and append to PATH variables. PATH points to binaries and libraries needed by hou module.
在Linux上,我们需要指定PYTHONPATH环境变量,Python使用该环境变量来查找模块。 在Windows上,我们需要指定PYTHONPATH并附加到PATH变量中。 PATH指向hou模块所需的二进制文件和库。

Add this to your VSCode configuration,
将此添加到您的VSCode配置,

Linux

"terminal.integrated.env.linux": {
    "PYTHONPATH" : "/opt/hfs16.5.473/houdini/python2.7libs"
}

Windows

"terminal.integrated.env.windows": { 
    "PYTHONPATH" : "C:\\Program Files\\Side Effects Software\\Houdini 16.5.473\\houdini\\python2.7libs",
    "PATH" : "${env:PATH};C:\\Program Files\\Side Effects Software\\Houdini 16.5.473\\bin"
}

After that it should be pretty comfortable to code with auto-completion and run Houdini Python scripts inside of Visual Studio Code.
之后,使用自动补全进行编码并在Visual Studio Code中运行Houdini Python脚本应该非常舒适。

Here are full configs for both Linux and Windows:
这是Linux和Windows的完整配置:

Linux

{
    "python.autoComplete.extraPaths": [ "/opt/hfs16.5.473/houdini/python2.7libs" ],
    "python.autoComplete.preloadModules": ["hou"],
    "terminal.integrated.env.linux": {
        "PYTHONPATH" : "/opt/hfs16.5.473/houdini/python2.7libs"
    }
}

Windows

{
    "python.autoComplete.extraPaths": [ "C:\\Program Files\\Side Effects Software\\Houdini 16.5.473\\houdini\\python2.7libs" ],
    "python.autoComplete.preloadModules": ["hou"],
    "terminal.integrated.env.windows": { 
        "PYTHONPATH" : "C:\\Program Files\\Side Effects Software\\Houdini 16.5.473\\houdini\\python2.7libs",
        "PATH" : "${env:PATH};C:\\Program Files\\Side Effects Software\\Houdini 16.5.473\\bin"
    }
}

That’s all for this post,
Happy coding 🙂

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值