CPython Traceback and loading error

Hello community! :wave:

I’m reaching out for some assistance as I’ve encountered a couple of diffculties while working with CPython on the latest version of PyRevit. I tried to test it pyrRevitDev and Test Cpython Command

1 - not being able to import some modules even if they are installed and work fine outside pyrevit.

2- CPython Traceback: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

I’ve attempted to install Python versions 3.7.8 and 3.8.5, and I’ve configured my environment variables, but the issue persists.

Has anyone else encountered a similar issue and successfully identified the root cause and solution? Any insights or assistance would be immensely appreciated!

Hi @YOUSSEF,

can you please test what does the following code outputs?

#! python3
import sys
print(sys.path)

if you don’t see the PYTHONPATH directory you have set, you’ve discovered a bug!
From a quick search through the source code it seems that the pythonpath variable is read but not used while loading the cpython engine.

EDIT: I was wrong, the variable is loaded correctly by pyRevit.

  1. the first problem should be mostly due to the order of which the search paths are added to sys.path; pyRevit’s own site-packages contains old/incompatible modules that gets loaded before the cpython 3 version.
  2. I was able to run the test without having that issue (but it crashes a bit after) on revit 2022, cpython 3.8.5. What’s your revit and pyrevit version?

Hi @sanzoghenzo

Thank you for response.
These are the outputs am getting but i am using python 3.7

#! python3
import sys
for i in sys.path:
    print(i)

>>> D:\__DEV__\pyArep\pyAREP_dev.extension\PyYoussef.tab\Test_tools.panel\Test.pushbutton
D:\__DEV__\Check-it\Check-it_lib.lib
D:\__DEV__\pyArep\pyAREP_lib.lib
C:\Program Files\pyRevit-Master\pyrevitlib
C:\Program Files\pyRevit-Master\site-packages
C:\Program Files\Python37\Lib\site-packages

even after switching the python path to first index in sys.path list i can still not get to load the packages :

#! python3
import sys
sys.path.insert(0, "C:\Program Files\Python37\Lib\site-packages")
for i in sys.path:
    print(i)
import numpy

>>>>C:\Program Files\Python37\Lib\site-packages
D:\__DEV__\pyArep\pyAREP_dev.extension\PyYoussef.tab\Test_tools.panel\Test.pushbutton
D:\__DEV__\Check-it\Check-it_lib.lib
D:\__DEV__\pyArep\pyAREP_lib.lib
C:\Program Files\pyRevit-Master\pyrevitlib
C:\Program Files\pyRevit-Master\site-packages
C:\Program Files\Python37\Lib\site-packages

CPython Traceback:
ModuleNotFoundError : No module named 'json'
 File "D:\__DEV__\pyArep\pyAREP_dev.extension\PyYoussef.tab\Test_tools.panel\Test.pushbutton\script.py", line 10, in <module>
import numpy
 File "C:\Program Files\Python37\Lib\site-packages\numpy\__init__.py", line 138, in <module>
 from ._version import get_versions
 File "C:\Program Files\Python37\Lib\site-packages\numpy\_version.py", line 7, in <module>
 import json

pyRevitLabs.PythonNet
à Python.Runtime.Runtime.CheckExceptionOccurred()
 à Python.Runtime.PyScope.Exec(String code, IntPtr _globals, IntPtr _locals)
 à Python.Runtime.PyScope.Exec(String code, PyDict locals)
 à PyRevitLabs.PyRevit.Runtime.CPythonEngine.Execute(ScriptRuntime& runtime)

I’m wondering if all this has somthing to do with this error :

PS : i have tried to figure it out with two possible python versions 3.7.8 and 3.8.5

Thanks again for you help !

Since python 3.7 reached is End of Life a while ago, I suggest you to move to 3.8.5 :wink:
The next major pyRevit Release will also drop old python versions and focus on newer ones.

In the meantime, if you feel brave enough you can try the WIP version, in which I triend to solve a long standing issue with CPython and fixed the import order (so you don’t have to worry about it if you use the PYTHONPATH environment variable)

https://github.com/eirannejad/pyRevit/actions/runs/8168659570/artifacts/1301311311

1 Like

Hi @sanzoghenzo,

Just a quick update after testing Pyrevit 4.8.15 WIP version.

Somehow i encounter an error when trying to launch a button for the first time after opening a new Revit session:

CPython Traceback: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

However, on the second attempt, the script worked fine. I managed to load all installed packages after reordering import paths as follows:

#! python3


# -*- coding: utf-8 -*-


 import sys
 sys.path.insert(0,r"C:\\Users\\mrabety\\AppData\\Roaming\\Python\\Python38\\site-packages")
 sys.path.insert(1,r"C:\Program Files\Python38\Lib")
 sys.path.insert(1,r"C:\Program Files\Python38\Lib")
 sys.path.insert(2,r"C:\Program Files\Python38\python38.zip")
 sys.path.insert(3,r"C:\Program Files\Python38\DLLs")
 sys.path.insert(4,r"C:\Program Files\Python38")
 sys.path.insert(5,r"C:\Users\mrabety\AppData\Roaming\Python\Python38\site-packages")
 for i in sys.path:
     print(i)

print("-----------------")
import numpy as np
print(np)

import pandas
print(pandas)

print('ok')
import openpyxl
print(openpyxl)

import plotly
print(plotly)

import sqlite3
print(sqlite3)

import clr

>>>>>>>
<module 'numpy' from 'C:\\Program Files\\Python38\\Lib\\site-packages\\numpy\\__init__.py'>
<module 'pandas' from 'C:\\Program Files\\Python38\\Lib\\site-packages\\pandas\\__init__.py'>
ok
<module 'openpyxl' from 'C:\\Program Files\\Python38\\Lib\\site-packages\\openpyxl\\__init__.py'>
<module 'plotly' from 'C:\\Users\\mrabety\\AppData\\Roaming\\Python\\Python38\\site-packages\\plotly\\__init__.py'>
<module 'sqlite3' from 'C:\\Program Files\\Python38\\Lib\\sqlite3\\__init__.py'>


# import CLR Error
CPython Traceback:
Tentative de lecture ou d'écriture de mémoire protégée. Cela indique souvent qu'une autre mémoire est endommagée.

Despite this adjustment, the problem persists with importing clr to use the Revit API.

In addition , everything strangely changes after reloading Pyrevit, and the modules will not load anymore.


Thanks again for you help !

Mixing environments in the sys.path is a recipe for disaster!

  • drop all the sys.path.insert lines;
  • install your requirements in single python 3.8.5 environment;
  • either set the PYTHONPATH environment variable or a single sys.path.insert to the site-packages folder of that python 3.8.5 environment.

The wip version should already take care of loading the right paths.

A side note: when you use the raw string format (the r before the double quotes you don’t need to escape the backslashes.

Having that said, and as I’ve stated many times here in the forum, cpython support is basically broken in pyrevit, and will hopefully be fixed in the next major release

1 Like