chianti-atomic / ChiantiPy

ChiantiPy is a python package to calculate the radiative properties of astrophysical plasmas based on the CHIANTI atomic database
63 stars 32 forks source link

Issue with io.defaultsRead #465

Open slimguat opened 1 month ago

slimguat commented 1 month ago

Upon importing ChiantiPy.tools.data I have this error


TypeError Traceback (most recent call last) Cell In[4], line 1 ----> 1 import ChiantiPy.tools.data

File \SaffronDev\Lib\site-packages\ChiantiPy\tools\data.py:46 44 try: 45 Xuvtop = os.environ['XUVTOP'] ---> 46 Defaults = chio.defaultsRead() 47 Ip = chio.ipRead() 48 MasterList = chio.masterListRead()

File \SaffronDev\Lib\site-packages\ChiantiPy\tools\io.py:430, in defaultsRead(verbose) 428 print((' reading chiantirc file')) 429 config = configparser.RawConfigParser(initDefaults) --> 430 config.read(rcfile) 431 defaults = {} 432 for anitem in config.items('chianti'):

File C:\Python312\Lib\configparser.py:681, in RawConfigParser.read(self, filenames, encoding) 679 encoding = io.text_encoding(encoding) 680 read_ok = [] --> 681 for filename in filenames: 682 try: 683 with open(filename, encoding=encoding) as fp:

TypeError: 'bool' object is not iterable

From what I understand, in io.py>defaultsRead function rcfile is False and right after there is a check for a file existance os.path.isfile(rcfile) that actually checks with rcfile being False. so I think there might be a need to change rcfile to None instead or change the check from if os.path.isfile(rcfile): to if os.path.isfile(rcfile) and (rcfile!=False) :

I am on Windows 11 x64, Python 3.12.3 ChiantiPy 0.15.1

kdere commented 1 month ago

to clarify, do you have an chianitrc file and if so where it it?

slimguat commented 1 month ago

I don't know what is this file exactly and where it is supposed to be. So where should I search for it? BTW: I work with the database CHIANTI_10.0.2_database that's in my C:/ drive.

kdere commented 1 month ago

It is a file that set some default values. You have to actually put it in the #HOME/.config or the #HOME/.chianti directory manually. Since you have not done this you do not have the file.

I have not run chiantipy under 3.12 but will try that this morning. I do not have any problems with 3.10 or 3.11

Ken

kdere commented 1 month ago

I can not reproduce this under Python 3.12 so I am puzzled.

I am using the latest CHIANTI version 10.1 but I also tried CHIANTI version 10.0 and things still worked.

My best suggestion is to see if editing your version of defaultsRead works for you

Ken

kdere commented 4 weeks ago

another thing you might try is to see if XUVTOP is properly set

import ChiantiPy.tools.constants as chconst

and

import ChiantiPy.tools.io as chio
lvl = chio.elvlcRead('c_4')
slimguat commented 4 weeks ago

I believe I have identified the root of the issue. It appears to be related to the Jupyter notebook environment I am using with Python 3.12. Specifically, the Jupyter server is incorrectly identifying the existence of a file when the os.path.isfile function is called with a boolean False value. This behavior seems to occur because, in Unix systems, the boolean False is equivalent to 0, which in turn corresponds to '/' (the root directory). As a result, os.path.isfile(False) is returning True.

However, this issue appears to be specific to Jupyter notebooks running Python 3.12 or later. I did not observe the same behavior when running the script directly in a terminal. Therefore, I believe this issue is not related to ChiantiPy, but rather to the interaction between Python 3.12 and the Jupyter environment.

kdere commented 4 weeks ago

I am running python 3.12.4 in a virtulall environment and do no have the problem. I checked my PYTHONPATH and it looks pretty clean. and, yes I do run linux.

thanks for letting me know.

Ken