# Removing envs that crash script editor

**URL:** <https://discourse.mcneel.com/t/removing-envs-that-crash-script-editor/192208>\
**Category:** Scripting\
**Tags:** windows, python\
**Created:** [October 1, 2024, 8:26pm UTC](https://discourse.mcneel.com/t/removing-envs-that-crash-script-editor/192208 "2024-10-01T20:26:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mat\_S](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/mat_s/32/61837_2.png) [@Mat\_S](https://discourse.mcneel.com/u/Mat_S)\
**Post date:** [October 1, 2024, 8:26pm UTC](https://discourse.mcneel.com/t/removing-envs-that-crash-script-editor/192208/1 "2024-10-01T20:26:25Z")

</div>

I am trying to install packages to a virtual environment, and then uninstall them when I made a mistake.

In the script editor, I have found the Open Python 3 Shell. When running python there, it doesn’t have numpy installed, so I tried looking for a venv activate file, but the `site-envs` seems to just be a folder.

From what I gather, there aren’t really virtual environments, just directories with names on them.

The issue I have is managing the `env: ` directive at the top of a python script. This seems to be persistent, or is corrupting something. I’m not sure what about running a script is causing the issue, but it essentially is:

A local project folder that has scripts and calls a module in that folder.

- Open one of those scripts in the Script editor, run it.
  - this works fine. I suspect because it is using the file/script directory (maybe relative path context?)

- Copy that same files code, and put in the script editor directly to run.
  - to compensate for the local module, I add the path of the file i copied into `env: path/to/file`
  - this causes permanent crashing, until the `editor.json` is deleted

Related: [Rhino 8 Python Crashes](https://discourse.mcneel.com/t/rhino-8-python-crashes/191988)

---

<div class="post-metadata">

**Author:** ![eirannejad](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/eirannejad/32/477962_2.png) [@eirannejad](https://discourse.mcneel.com/u/eirannejad)\
**Post date:** [October 3, 2024, 5:00pm UTC](https://discourse.mcneel.com/t/removing-envs-that-crash-script-editor/192208/4 "2024-10-03T17:00:39Z")

</div>

### Package Environments:

You are correct. The subfolders under `site-envs` are not virtual environments. It is key to know that you are running in a single long-running process. So one a version of `numpy` libraries are loaded, loading another version causes conflicts.

👉 The `site-env` folders are designed to allow multiple package versions and their dependencies to be installed separately from each other. Each folder can contain a combination of packages required by one of your scripts. But you still need be aware of multiple versions of the same library loaded in Rhino:

Imagine this scenario:

- file `script_a.py` requires `numpy` version 1
- file `script_b.py` requires `pytorch` version 2 which is dependent on `numpy` version 2
- On a clean Rhino, running `script_a` installs and loads numpy v1
- On a clean Rhino, running `script_b` installs and loads pytorch + numpy v2

**Conflict:** : Currently running script\_b right after script\_a might cause conflicts since numpy v1 is already loaded. So when pip wants to uninstall numpy v1 to install numpy v2 on windows it fails to delete numpy’s native libraries that are loaded in Rhino memory as these files are locked.

**Workaround:** Specify a `venv: some-env-name` at the top of your scripts. This will create a subfolder under `site-env` for this script and installs the packages there. And since there is no numpy v1 is installed in this folder there is no install conflict.

Now all the packages required for the two scripts are installed. You still need to run each script in a fresh Rhino instance since you need to make sure two version of numpy are not loaded at the same time. But the separate environments cache the necessary packages so they don’t need to get installed-uninstalled every time you are running multiple packages with different package requirements.

⚠ I would advise against manually adding the paths to these folders to your script. Just use the `venv: ` specifier. Note that `env:` is different from `venv:`

### Python Shell

When using the python shipped with Rhino, in the python shell, and manually adding/removing packages, you need to specify the `--target` which is the subfolder inside `site-envs` directory. The prompt on the shell has a note for this. See highlighted text below:

 ![Screenshot 2024-10-03 at 9.59.18 AM](https://global.discourse-cdn.com/mcneel/uploads/default/original/4X/a/0/9/a09f08ee48001482f0ebecae79f739bccfab8273.png)

---

<div class="post-metadata">

**Author:** ![Mat\_S](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/mat_s/32/61837_2.png) [@Mat\_S](https://discourse.mcneel.com/u/Mat_S)\
**Post date:** [October 3, 2024, 6:02pm UTC](https://discourse.mcneel.com/t/removing-envs-that-crash-script-editor/192208/5 "2024-10-03T18:02:17Z")

</div>

There’s a bit to unpack here, but to start:

- It was not `script_a` then `script_b`, it was `script_a` twice in a row.

I think the terminal is confusing for me for a few reasons;

1. there is no `site-packages` under `libs` folder.
2. When running python.exe, I would expect the modules installed to be part of it. I guess this has something to do with this sort of runtime loading.  
a. but typically executing `python.exe` and in that environment I should `import numpy`, when it is part of the pip-installed modules  
b. since this is framed as ‘a single long-running process’, I would expect it to have all installed modules available
3. So, how could I start the python instance in the terminal, where it is pulling the modules from the folders?

* * *

Some unsolicited and uninformed , respectfully, thoughts/questions on, ‘why not do…’:

Maybe loaded modules can be tracked with modulefinder and/or sys dictionary, then 1. Raise a warning, but use an existing module instance instead of installing to a new ‘pseudo’ environment or 2. raise an exception when there are requirements set at the script level with conflicting module versions (i.e. numpy 1.12 is already loaded, but user puts `# r: numpy==1.13` in their own script.

---

<div class="post-metadata">

**Author:** ![eirannejad](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/eirannejad/32/477962_2.png) [@eirannejad](https://discourse.mcneel.com/u/eirannejad)\
**Post date:** [October 8, 2024, 12:07am UTC](https://discourse.mcneel.com/t/removing-envs-that-crash-script-editor/192208/6 "2024-10-08T00:07:21Z")

</div>

I just got this documented here with some sketches/images to explain. Take a look and see if it makes sense. I am all for making improvements and getting the package conflict issues resolved. You definitely should have a `site-packages` directory as `pip` and some other utilities are installed there. It is under `~/.rhinocode/py38-rh8/lib/python3.9/site-packages` on macOS and `%USERPROFILE%\.rhinocode\py38-rh8\lib\site-packages` on Windows:

> **[Python Package Environments](https://developer.rhino3d.com/guides/scripting/advanced-pyvenvs/)**
>
> Provides information on managing multiple python package environments

---

<div class="post-metadata">

**Author:** ![Mat\_S](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/mat_s/32/61837_2.png) [@Mat\_S](https://discourse.mcneel.com/u/Mat_S)\
**Post date:** [October 10, 2024, 1:50pm UTC](https://discourse.mcneel.com/t/removing-envs-that-crash-script-editor/192208/7 "2024-10-10T13:50:17Z")

</div>

I think I was just confused with your terminal output, since I am on windows.

 ![image](https://global.discourse-cdn.com/mcneel/uploads/default/original/4X/2/8/1/281b921a88f49b358bb42b50d35176003b286190.png)

As for the guide, which is really nice;  
The first paragraph of this section [Rhino - Python Package Environments](https://developer.rhino3d.com/guides/scripting/advanced-pyvenvs/#virtual-environments) that threw me off, since I didn’t realize it wasn’t talking about Rhino yet. I was looking for site-packages in the folder where the pseudo virtual environment was.

Maybe just rephrasing to start with:

> Unlike the implementation used in Rhino, users may be familiar with tools like `pipenv` and `venv` environment managers for Python, which normally solve this problem by creating multiple **Virtual Environment** s

In [Rhino - Python Package Environments](https://developer.rhino3d.com/guides/scripting/advanced-pyvenvs/#custom-environments)

> Python 3 will create new folder `site-envs/my-pytorch-tools-<unique-id>` to install pytorch v2.4.1 in that environment. `<unique-id>` is automatically assigned to the environment to avoid conflicts:

It’s not obvious to me when a _new_ environment is created, and when a `unique-id` is appended. If I use `# venv: just-for-me` , it makes a folder like `./just-for-me-0x2340sdfjl`. So when I run it again, or make a new script, or open rhino again, and still have `# venv: just-for-me`, does it use `./just-for-me-0x2340sdfjl` or makes a new one with a unique id `./just-for-me-sdl8280jl` . How would I know this, and why would the unique id get appended at all if it just references the name I use to begin with.

> In case any install errors occured, please run _Tools \> Advanced \> Reset Python 3 (CPython) Runtime_ to reset the complete runtime and install a fresh deployment of Python 3 with clean `site-packages` and `site-envs` directories.

Not sure if it’s the explanation or I have a different issue, but often this reset doesn’t work, I just get an error that the environment is corrupted, and only way to solve it is restarting Rhino, not just resetting python.

---

<div class="post-metadata">

**Author:** ![eirannejad](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/eirannejad/32/477962_2.png) [@eirannejad](https://discourse.mcneel.com/u/eirannejad)\
**Post date:** [October 10, 2024, 10:23pm UTC](https://discourse.mcneel.com/t/removing-envs-that-crash-script-editor/192208/8 "2024-10-10T22:23:35Z")

</div>

- Script specifying `just-for-me` always uses the `just-for-me-<unique-id>` that was created for it on the first run. This way the required packages for this script do not need to be installed every time
- Yes if there is installation errors, the environment is marked as invalid and Rhino needs to be restarted. `Tools > Advanced > Reset Python 3 (CPython)` resets the complete python runtime including all the various environments and everything else in the folder (still needs Rhino restart)

---

<div class="post-metadata">

**Author:** ![Matt\_Church](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/matt_church/32/663903_2.png) [@Matt\_Church](https://discourse.mcneel.com/u/Matt_Church)\
**Post date:** [June 24, 2025, 10:09am UTC](https://discourse.mcneel.com/t/removing-envs-that-crash-script-editor/192208/10 "2025-06-24T10:09:26Z")

</div>

This is was super helpful!

I’ve been getting the “_Error building script: Environment “default” is corrupted due to a possible package installation failure_” error and wasn’t sure how to fix it as I missed that you could specify separate environments within the script editor.

---

<div class="post-metadata">

**Author:** ![eirannejad](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/eirannejad/32/477962_2.png) [@eirannejad](https://discourse.mcneel.com/u/eirannejad)\
**Post date:** [June 26, 2025, 4:07pm UTC](https://discourse.mcneel.com/t/removing-envs-that-crash-script-editor/192208/11 "2025-06-26T16:07:28Z")

</div>

Restarting Rhino would recreate the environment. This usually happens when a package install fails. If not, use _Tools → Advanced → Reset Python 3 Runtime_ and restart Rhino.
