Error when re-opening Revit

Hi everybody!
I’m new to pyRevit, so I’d like to apologize if I’m doing this wrong.
I’m trying to create a code to insert the pipe flow rate through a calculation on a shared parameter in the pipe family.
The first time I ran the code it worked fine. However, when I reopened Revit, the code started showing two errors ( I will put their printouts here and the code as well). I don’t know what can be happening. If someone can help, I would be very grateful.

#! python3

# Carregar as bibliotecas DesignScript e padrão do Python
import sys
import clr
import numpy as np
import math
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

#Importando RevitNodes
clr.AddReference("RevitNodes")
import Revit

#Importando Revit API
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

#Importando o Gerenciador de socumentos e de transações
clr.AddReference("RevitServices")
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = __revit__.ActiveUIDocument.Document


Tubos = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_PipeCurves).WhereElementIsNotElementType().ToElements()

#print(Tubos)

Q = [0.0,0.0]
n = 0.011
j = 0


for i in Tubos:
    Dim = i.LookupParameter('Diâmetro')
    Dim = Dim.AsDouble()*(0.3048)
    print('O diâmetro é:\n',Dim)
    Rh = Dim/4
    print('O raio hidráulico é igual a:\n',Rh)
    A = ((math.pi)*(Dim**2))/4
    print('Área igual a:',A)
    s = i.LookupParameter('Inclinação') #s = inclinação
    s = s.AsDouble()
    print('A inclinação do tubo é:\n',s)
    Q[j] = ((A)*(Rh**(2/3))*(s**(1/2)))/n #Q = Seção Plena
    j = j + 1
    print('A capacidade do tubo em seção plena é de:\n',Q)
    Vp = (((A)*(Rh**(2/3))*(s**(1/2)))/n)/A #Vp = Velocidade em Seção Plena
    print('A velocidade em seção plena é de:\n',Vp)
    print('\n\n')

#Inicinado ação
t = Transaction(doc, "Vazão")
t.Start()
j = 0
for i in Tubos:
    V = i.LookupParameter('Q - Vazão Seção Plena')
    if V:
        V.Set(Q[j])
        j = j + 1
#Fim do programa
t.Commit()

try removing this :point_up:
you don’t seem to use it and it seems to be the issue.

1 Like

Hi, thanks for responding!
I removed it as you suggested, but now it generates other errors. I tried to make a simple code, but it still gives errors.

#! python3

import sys

print(sys.version)

Error:

CPython Traceback:
AttributeError : ‘ScriptIO’ object has no attribute ‘write’
File “C:\Users\willian.silva\OneDrive - Bild Desenvolvimento Imobiliário Ltda\Área de Trabalho\TCC\TCC.extension\TCC.tab\TCC.panel\Vazão.pushbutton\script.py”, line 5, in
print(sys.version)
pyRevitLabs.PythonNet
em Python.Runtime.Runtime.CheckExceptionOccurred()
em Python.Runtime.PyScope.Exec(String code, IntPtr _globals, IntPtr _locals)
em Python.Runtime.PyScope.Exec(String code, PyDict locals)
em PyRevitLabs.PyRevit.Runtime.CPythonEngine.Execute(ScriptRuntime& runtime)

and without this it works right?

looking at you code, you don’t need to work with cpython / python3, so just remove the first line and that should work accordingly

1 Like

You really didn’t need it! I removed it, now it’s running. Thank you very much!

But now why can’t I import the numpy ?

I ran a test script, look what happened:

#! python3
# -*- coding: utf-8 -*-

import sys
print(sys.version)
print('\n## sys.path')
print('\n'.join(sys.path))

def print_html(output_str):
    print(output_str.replace('<','&clt').replace('>','&cgt'))


try:
    import numpy as np
    print('\n numpy array:')
    print(repr(np.arange(15).reshape(3 ,5)))
except Exception as ex:
    print('numpy load error {}'.format(ex))

try:
    import pandas as pd

    df_dict = {'key 1': 1,'key 2': 2, 'key 3': 3}
    df = pd.DataFrame([df_dict])

    print("\n## pandas DataFrame:")
    print_html(df.to_html().replace('\n',''))
except Exception as ex:
    print(f'pandas load error: {ex}')

Output:

3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)]
sys.path:
C:\Users\willian.silva\OneDrive - Bild Desenvolvimento Imobiliário Ltda\Área de Trabalho\TCC\TCC.extension\TCC.tab\Teste.panel\Teste.pushbutton
C:\Program Files\pyRevit-Master\pyrevitlib
C:\Program Files\pyRevit-Master\site-packages
C:\Users\willian.silva\AppData\Local\Programs\Python\Python38\Lib\site-packages
numpy load error No module named ‘glob’
pandas load error: Unable to import required dependencies:
numpy: No module named ‘glob’
pytz: No module named ‘datetime’

Do you know how I can fix it?

you may want to create a new topic to make it easier for others in the forum.
There are a few post about that subject if you look for ‘numpy’

2 Likes

I faced a similar issue:

  1. Make sure I had python 3.8.5 installed
  2. Solution was to set a user environment variable to the pyRevit cpython engine