2

I want to build physics simulations using VPython. Although, VPython works great on websites like trinket.io, I'd like to use a nicer IDE than the web one, and I'd like to use real python modules as well as external libraries like numpy.

I followed the instructions at https://vpython.org/presentation2018/install.html to install VPython locally. I used the command pip install vpython (not really a fan of conda) and wrote this simple demo program:

from vpython import sphere

sphere()

When I ran this from the command line, the program indeed worked: a browser window opened displaying the sphere. However, there are several issues with this setup:

  • When looking at the console, it keeps throwing a weird error RuntimeError: can't register atexit after shutdown. However, it still seems to run fine.
  • I couldn't find a way to nicely stop the simulation. Closing the browser tab doesn't exit the program. Pressing Ctrl+C also doesn't do anything useful. The only way to stop it is to use Ctrl+Break, which is a really dirty way, in my opinion. I tried to use stop_server as documented in that link, but no result.

Is there a way to fix these issues or maybe a different environment that will answer my needs? I really want to be able to use built-in python modules and external libraries like numpy but it seems impossible at the moment.

2
  • 1
    I think it is indeed the case that running from a terminal is awkward. Launching from an IDE such as Spyder will be much more satisfactory. Commented Oct 23, 2021 at 3:39
  • 1
    I'll add that at matterandinteractions.org/student are 70 physics programs that run in the browser (using GlowScript VPython). They may be of interest to you. Commented Oct 23, 2021 at 3:41

4 Answers 4

1

I was getting the same error when using a recursive function to create shapes. The recursive threads were still running when vpython exited.

You can prevent the vpython server from shutting down until you exit the program by adding these lines after you set up your scene and draw your shapes:

while True:
    rate(60)

This will keep the connection to the vpython server active as long as the program is running (while True) and set the frame rate to 60fps (rate(60))

This comes from a note in the vpython documentation specific to running instances of vpython from the terminal:

When running from a terminal, if the program does not end with a loop containing a rate() statement, you need to add "while True: rate(30)" to the end of the program. This is not necessary when launching from environments such as Jupyter notebook, IDLE, or Spyder.

https://vpython.org/presentation2018/install.html

Sign up to request clarification or add additional context in comments.

Comments

0

It's a matter of Python versions as per this discussion.

There was a discussion on a mismatch between the vpython release and the used python interpretter:

We are behind. The builds for 7.6.1 were done for Python 3.6, 3.7, and 3.8, not 3.9. So it might be that it would work if you downgraded Python. Bruce

You're correct, thanks! I downgraded to 3.8.6 and it worked.

Comments

0

To exit the program, you need to stop the terminal by entering the exit command.

Comments

0

In addition to adding the rate(60) code as described above, to terminate the program I had to issue a kill command from another terminal. Pressing the Ctrl-break key didn't do anything.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.