简体   繁体   English

在 PyCharm 中使用 VPython

[英]Using VPython in PyCharm

My Physics class requires me to use VPython for making models and such.我的物理课要求我使用VPython来制作模型等。 I love the idea of incorporating code into Physics and VPython seems to be pretty good overall, but VPython really wants you to use VIDLE, their version of IDLE, as your IDE.我喜欢将代码合并到 Physics 中的想法,总体而言 VPython 似乎非常好,但 VPython 真的希望您使用 VIDLE,他们的 IDLE 版本,作为您的 IDE。

I am trying to use it in my favorite Python IDE, PyCharm .我正在尝试在我最喜欢的 Python IDE PyCharm 中使用它。 If I run a script that uses VPython modules that I know works in VIDLE, I get an error:如果我运行的脚本使用我知道在 VIDLE 中有效的 VPython 模块,我会收到错误消息:

ImportError: No module named visual

I can go to PyCharm's Project Interpreter page, where it appears that I'm supposed to be able to add and remove modules, but I can't figure out how to do it.我可以转到 PyCharm 的 Project Interpreter 页面,在那里我似乎应该能够添加和删除模块,但我不知道该怎么做。 If I click Add/Install Package it brings up a searchable list of tons of available packages (from PyPi database, right?), but VPython is not on the list.如果我单击添加/安装包,它会显示一个可搜索的大量可用包列表(来自 PyPi 数据库,对吗?),但 VPython 不在列表中。

It appears that VPython is made up of 3 modules called "vis", "visual", and "visual_common" and also installs other modules "numpy" (already installed), "FontTools", "Polygon", and "ttfquery".看起来 VPython 由 3 个模块组成,称为“vis”、“visual”和“visual_common”,并且还安装了其他模块“numpy”(已安装)、“FontTools”、“Polygon”和“ttfquery”。

Any ideas?有任何想法吗? Hopefully it's just something simple.希望这只是一些简单的事情。

Unfortunately, you cannot install vpython as easy as normal python packages.不幸的是,你不能像普通的 python 包那样简单地安装 vpython。 The process is a lot more involved than that.这个过程比这要复杂得多。 If you want to develop using PyCharm though, you still can.如果你使用 PyCharm 进行开发,你仍然可以。

First and foremost, you need to install vpython on windows.首先,您需要在 Windows 上安装 vpython。 This will likely install itself as one of your main python installation's site packages.这可能会将自身安装为您的主要 python 安装站点包之一。

Install it from here -> http://vpython.org/contents/download_windows.html从这里安装 -> http://vpython.org/contents/download_windows.html

After doing so, simply chose the python installation that has vpython installed.这样做之后,只需选择安装了 vpython 的 python 安装。

If you wish to create a virtualenv, then do so with the --system-site-packages :如果你想创建一个 virtualenv,那么使用--system-site-packages来做:

 $ virtualenv --help                                                                                         [12:51:06]
Usage: virtualenv [OPTIONS] DEST_DIR

Options:
  --version             show program's version number and exit
  (...)
  --no-site-packages    DEPRECATED. Retained only for backward compatibility.
                        Not having access to global site-packages is now the
                        default behavior.
  --system-site-packages
                        Give the virtual environment access to the global
                        site-packages.
  (...)

You can get the vpython working in python3.4 now.你现在可以让 vpython 在 python3.4 中工作了。 Follow this steps:请按照以下步骤操作:

Preparation准备

Download the four packages TTFQuery , FontTools , and Polygon , vpython in http://www.lfd.uci.edu/~gohlke/pythonlibs/ to same directory.http://www.lfd.uci.edu/~gohlke/pythonlibs/ 中的四个包TTFQueryFontToolsPolygonvpython下载到同一目录。

Installation安装

  1. Run cmd运行命令
  2. cd path/to/package_downloaded_directory
  3. pip install packagename.whl
  4. Find the file C:\\Python34\\lib\\site-packages\\vis\\materials.py找到文件C:\\Python34\\lib\\site-packages\\vis\\materials.py
  5. Open the file in an editor, then go to line 70在编辑器中打开文件,然后转到第 70 行
  6. Comment the two lines just like this像这样注释这两行
class raw_texture(cvisual.texture): def __init__(self, **kwargs): cvisual.texture.__init__(self) # for key, value in kwargs.items(): # self.__setattr__(key, value)
  1. Save the changes保存更改

Note that when when you use vpython to code a script, the first line must be like below:请注意,当您使用 vpython 编写脚本时,第一行必须如下所示:

from vis import *

Below is my code sample下面是我的代码示例

from vis import *  
sphere(pos=vector(0,0,0),radius=0.5,color=color.red)
arrow(pos=vector(0.5,0,0),axis=vector(1,0,0),color=color.green)

Before using the package you need to install the package vpython in Pycharm.在使用包之前,您需要在 Pycharm 中安装包 vpython。

from vpython import * ball = sphere()从 vpython 导入 * 球 = 球体()

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

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