CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Running, Solving & CFD

[pyFoam][Solution] Error when importing pyFoam modules on Spyder IDE

Register Blogs Community New Posts Updated Threads Search

Like Tree2Likes
  • 2 Post By gabrielfelix

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   September 15, 2021, 09:59
Default [pyFoam][Solution] Error when importing pyFoam modules on Spyder IDE
  #1
Member
 
Gabriel Felix
Join Date: May 2021
Location: Brazil
Posts: 35
Rep Power: 5
gabrielfelix is on a distinguished road
Hi folks,

I have been using nano to edit my pyFoam scripts and I wanted to use an IDE for the script edition because the python console would help me a lot to test the functions while programming the code.

So I loaded my code in Spyder, however, I was getting an error related to importing the pyFoam modules when running the code.
Code:
[autoreload of PyFoam.Infrastructure.Configuration failed: Traceback (most recent call last):
  File "C:\Users\Felix\anaconda3\lib\site-packages\IPython\extensions\autoreload.py", line 245, in check
    superreload(m, reload, self.old_objects)
  File "C:\Users\Felix\anaconda3\lib\site-packages\IPython\extensions\autoreload.py", line 394, in superreload
    module = reload(module)
  File "C:\Users\Felix\anaconda3\lib\imp.py", line 314, in reload
    return importlib.reload(module)
  File "C:\Users\Felix\anaconda3\lib\importlib\__init__.py", line 169, in reload
    _bootstrap._exec(spec, module)
  File "<frozen importlib._bootstrap>", line 604, in _exec
  File "<frozen importlib._bootstrap_external>", line 843, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\Felix\anaconda3\lib\site-packages\PyFoam\Infrastructure\Configuration.py", line 85, in <module>
    "descriptionPath": eval('["'+path.curdir+'","'+path.join(userDirectory(),"caseBuilderDescriptions")+'","'+path.join(globalDirectory(),"caseBuilderDescriptions")+'"]'),
  File "<string>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
]
As I saw in this post the error is related to the use of backslashes in python vs. windows. A classical error as I learned somewhere else.

And as it can be seen, when evaluating the userDirectory and globalDirectory variables contained in the File "C:\Users\Felix\anaconda3\lib\site-packages\PyFoam\Infrastructure\Configuration.py", there are indeed slashes and backslashes mixed.

Code:
In[43]: userDirectory()
Out[43]: 'C:\\Users\\username\\.pyFoam'
In[44]: globalDirectory()
Out[44]: '/etc\\pyFoam'
What I did to solve the problem was to replace the "" with "/" in the variables using the .replace('\\','/') function. The caseBuilder line in the Configuration.py file then became:
Code:
"CaseBuilder":{
        "descriptionPath": eval('["'+path.curdir+'","'+path.join(userDirectory().replace('\\','/'),"caseBuilderDescriptions")+'","'+path.join(globalDirectory().replace('\\','/'),"caseBuilderDescriptions")+'"]'),
    },
Which solved the problem and the modules were loaded perfectly after. Then I could use every pyFoam modules and functions in Spyder.
GreenEntropy and gimson like this.

Last edited by gabrielfelix; September 15, 2021 at 12:54.
gabrielfelix is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 15:36.