vpython in a virtual environment

576 views
Skip to first unread message

Stuart Spaven

unread,
Feb 8, 2022, 1:13:26 PM2/8/22
to VPython-users
I am running python 3.9.9 I installed and ran vpython in a global environment. I am following Paul McWhorter on youtube and set up a virtual environment. I ran script activate and pip install vpython. I also installed pyserial in thie virtual environment. Pyserial runs fine but vpython will not. When I run a program with vpython the browser opens up but nothing appears. When I get out of the browser I do not show any errors in the terminal. I should also mention I am creating my programs in VSCode.

Bruce Sherwood

unread,
Feb 8, 2022, 3:05:46 PM2/8/22
to VPython-users
I don't know anything about pyserial. 

How do you launch the vpython program?

Bruce

Stuart Spaven

unread,
Feb 8, 2022, 7:43:57 PM2/8/22
to VPython-users
from vpython import *
myBox = box(length=2,width=1,height=2, color=color.red)

while True:
    pass
Above is sample code that runs in the global environment but has issues with a virtual environment.I am running vs code so I hit the green arrow when ready to run
A browser opens but I just have a white screen. In the global environment I get a red box in a browser window.

Bruce Sherwood

unread,
Feb 8, 2022, 8:12:21 PM2/8/22
to VPython-users
Sorry, but I don't know in what application "the green arrow" is found. What application do you use to launch the vpython program?

Bruce

Stuart Spaven

unread,
Feb 8, 2022, 10:05:03 PM2/8/22
to VPython-users
I am using Virtual Studio Code(vscode) as the IDE

Bruce Sherwood

unread,
Feb 8, 2022, 10:22:01 PM2/8/22
to VPython-users
I apologize for not reading carefully your program. Delete the last two lines. They constitute an infinite loop that prevents rendering the scene. Moreover, they aren't necessary. 

If you do eventually write an animation loop,  be sure to include a rate(number of renders per second) statement in the loop. 

Bruce 


Stuart Spaven

unread,
Feb 9, 2022, 12:21:33 PM2/9/22
to VPython-users
I deleted the while loop and the program still would not run in a virtual environment. The program runs with or without the while loop in standard python.

Bruce Sherwood

unread,
Feb 9, 2022, 2:41:09 PM2/9/22
to VPython-users
I have a recent installation of Anaconda, with Python 3.9. I made an environment that contains Python 3.8 and your program runs fine from idle and from spyder in that environment. I don't know how one launches a Python program from Visual Studio, so I didn't try that.

My mistake in saying that the infinite loop would block updating the screen in the absence of a rate function. That is the case in Web VPython. It is of course the case that one almost always needs a rate statement in a loop that updates the scene, because otherwise the updates may occur too fast to see.

Bruce

John

unread,
Feb 10, 2022, 2:59:10 PM2/10/22
to VPython-users
Hi Bruce

   This video tutorial by Paul McWhorter shows how to run vpython from Visual Studio Code.


   I am not a user of Visual Studio Code but I was able to get vscode to run a vpython program from a virtual environment. The first thing to check is that you can launch a vpython program from the command line in your virtual environment without using Visual Studio Code. Just open a terminal, activate the environment that you have vpython installed in and run run the program from the command line.

     python  mydemo.py

where mydemo.py contains your vpython program. In my case it contained:

from vpython import *
import sys
print(sys.version)
box()

 Check that this works first. If this works then you know that vpython works in your virtual environment. Then try launching Visual Studio Code from the terminal window from this virtual environment with the command

     code

This will launch Visual Studio code from that environment and then try running the program from Visual Studio Code. This worked for me but I was using Anaconda Python to create the virtual environment rather than the default python..

John

Bruce Sherwood

unread,
Feb 10, 2022, 11:07:09 PM2/10/22
to VPython-users
I have to give up. None of the recommended ways to run from VSC works for me. I do have two questions:

1) Stuart, EXACTLY what to you do to run from Visual Studio?

2) Does a blank browser page get displayed? If so, please press ctrl-shift-j (if using Chrome or Edge) and report what you see.

Bruce

Lee harrell

unread,
Feb 11, 2022, 6:52:51 AM2/11/22
to vpytho...@googlegroups.com
When you get the blank browser have you tried refreshing the page? Sometimes the browser opens before the server is ready and the page needs to be reloaded.

Lee



Sent from my iPhone

On Feb 10, 2022, at 11:07 PM, Bruce Sherwood <bruce.s...@gmail.com> wrote:

I have to give up. None of the recommended ways to run from VSC works for me. I do have two questions:

1) Stuart, EXACTLY what to you do to run from Visual Studio?

2) Does a blank browser page get displayed? If so, please press ctrl-shift-j (if using Chrome or Edge) and report what you see.

Bruce

--
You received this message because you are subscribed to the Google Groups "VPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vpython-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vpython-users/de2b5276-a82a-414f-bc23-62e2b9ea1aean%40googlegroups.com.

Lee harrell

unread,
Feb 11, 2022, 6:53:02 AM2/11/22
to vpytho...@googlegroups.com

John

unread,
Feb 11, 2022, 11:54:29 AM2/11/22
to VPython-users
Hi Bruce

    You need to set up Visual Studio Code to use python. See this video on how to set it up if you are using Anaconda.


   You can start at around the 3 minute mark if you already have Anaconda installed. You will need to install the python extension for Visual Studio Code from Microsoft in order to use python in Visual Studio Code as explained at the 3 minute mark of the video. Make sure you get the one from Microsoft.

This follow up video talks about how to switch python environments starting around the 8 minute mark.


John

Bruce Sherwood

unread,
Feb 11, 2022, 11:59:37 AM2/11/22
to VPython-users
Alas, John, I did try to do all those things, but after repeated trials I simply was unable to install the Python extension.

Bruce

Stuart Spaven

unread,
Feb 11, 2022, 2:07:33 PM2/11/22
to VPython-users
I set up a virtual environment in the command window, I used your program and the browser opened with a blank window just as it did in vs code. When I deactivated the virtual environment the window opened with the box as it should. I'm trying to find out why the code runs in standard mode but not virtual mode. John I am following Paul McWhorter and followed his instruction for setting up a virtual environment in vscode,  I am not using anaconda.

Stuart Spaven

unread,
Feb 11, 2022, 2:11:32 PM2/11/22
to VPython-users
Lee, I have tried refreshing the screen to no avail. Virtual Studio Code is an IDE from Microsoft. I can be installed with or without Anaconda. I pressed ctrl-shift-j and got the following messages:
START OF GLOWCOMM
(index):934 END OF GLOWCOMM
localhost/:1
       
       Unchecked runtime.lastError: The message port closed before a response was received.
(index):47 SERIF FONT LOADED
(index):36 SANS-SERIF FONT LOADED
glow.min.js:1
       
       Uncaught Error: Can't create canvas: WebGL not supported
    at new WebGLRenderer (glow.min.js:1:276226)
    at canvas.__activate (glow.min.js:1:253389)
    at init (glow.min.js:1:391803)
    at initObject (glow.min.js:1:393284)
    at new box (glow.min.js:1:406527)
    at initObject (glow.min.js:1:392623)
    at box (glow.min.js:1:406527)
    at handle_cmds (localhost/:654:43)
    at handler (localhost/:546:55)
    at WebSocket.ws.onmessage (localhost/:74:4)

On Friday, February 11, 2022 at 6:53:02 AM UTC-5 lee.e....@gmail.com wrote:

John

unread,
Feb 11, 2022, 3:39:40 PM2/11/22
to VPython-users
You should check to see if vpython runs in your virtual environment without using Visual Studio Code.  Just open up a terminal window on your computer and activate your virtual environment that has vpython installed in it 
 
On windows that would be something like

myenv\Scripts\activate.bat

On Linux or Mac it would be

$ source myvenv/bin/activate 

Where myenv is the name of your virtual environment. Then once your virtual environment is activated just run your vpython program from the command line.

  python myDemo.py

Where myDemo.py contains your vpython program. See if this works without using Visual Studio Code. 

John

Bruce Sherwood

unread,
Feb 11, 2022, 4:05:13 PM2/11/22
to VPython-users
That's really strange getting an error message that WebGL isn't available, since it is built into all modern browsers, and you were able to run in your other configuration.

Bruce
Reply all
Reply to author
Forward
0 new messages