desihub / speclite

Lightweight utilities for working with spectroscopic data
14 stars 19 forks source link

Error in speclite/filters.py with Python 3.5.2 :: Anaconda 4.2.0 (64-bit) #26

Closed julienguy closed 4 years ago

julienguy commented 7 years ago

I have the following error with Python 3.5.2 :: Anaconda 4.2.0 (64-bit)

File "/home/guy/software/DESI/speclite/speclite/filters.py", line 1277, in init if isinstance(response, basestring): NameError: name 'basestring' is not defined

julienguy commented 7 years ago

replacement with isinstance(entry,str) works.

dkirkby commented 7 years ago

Thanks for reporting this. I don't think your fix is backwards compatible to python 2.7, but python 3.x compatibility should be handled automatically at the time you install the package so I am guessing you are running from a git clone without running setup.py? If so, try running:

python setup.py install

and see if that fixes the problem. I can probably fix this so the install setup is not necessary, but it would be useful to first confirm if this is the problem.

julienguy commented 7 years ago

Yes. I was running from a git clone. I checked that going back to the original code and doing the setup.py install fixed the issue.

However, it's not totally straightforward. In order to install with setup.py in a custom directory (not to mix different sources), I had first to edit my PYTHONPATH before installing to avoid curious error messages. Just to give you all the info. The following works:

export PYTHONPATH=/home/guy/software/DESI/speclite-install/lib/python3.5/site-packages:$PYTHONPATH
python setup.py install --prefix /home/guy/software/DESI/speclite-install

But this doesn't work:

python setup.py install --prefix /home/guy/tmp

unning install
Checking .pth file support in /home/guy/tmp/lib/python3.5/site-packages/
/home/guy/software/DESI/anaconda3/bin/python -E -c pass
TEST FAILED: /home/guy/tmp/lib/python3.5/site-packages/ does NOT support .pth files
error: bad install directory or PYTHONPATH

You are attempting to install a package to a directory that is not
on PYTHONPATH and which Python does not read ".pth" files from.  The
installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:

    /home/guy/tmp/lib/python3.5/site-packages/

and your PYTHONPATH environment variable currently contains:

    '/home/guy/software/DESI/redmonster/python/:/home/guy/software/DESI/teststand/py:/home/guy/software/DESI/desitarget/py:/home/guy/software/DESI/zztop/py:/home/guy/software/DESI/desiutil/py:/home/guy/software/DESI/speclite-install/lib/python3.5/site-packages:/home/guy/software/DESI/specsim:/home/guy/software/DESI/desispec/py:/home/guy/software/DESI/desimodel/py:/home/guy/software/DESI/desisim/py:/home/guy/software/DESI/specter/py:/home/guy/software/DESI/specex/python:/home/guy/software/DESI/anaconda3'

Here are some of your options for correcting the problem:

* You can choose a different installation directory, i.e., one that is
  on PYTHONPATH or supports .pth files

* You can add the installation directory to the PYTHONPATH environment
  variable.  (It must then also be on PYTHONPATH whenever you run
  Python and want to use the package(s) you are installing.)

* You can set up the installation directory to support ".pth" files by
  using one of the approaches described here:

  https://setuptools.readthedocs.io/en/latest/easy_install.html#custom-installation-locations

Please make the appropriate changes for your system and try again.
weaverba137 commented 6 years ago

I just want to note that having python setup.py install deal with instances of basestring is not necessarily future-proof. If this capability is provided by astropy, then it may go away as soon as right now, since Astropy does not support Python 2 after 3.0.

moustakas commented 6 years ago

I just ran into the same issue, namely, NameError: name 'basestring' is not defined. I tried cloning the master repo and added that to my PYTHONPATH by hand and I also tried cloning and then running python setup.py install, and ran into the same issue. (Installing the last stable version using pip works, but I need the filters added in #37.)

I'll change basestring to str in my local checkout, but it would be nice to not to have to do this! Maybe even a simple try, except?

I'm using python version 3.6.5.

dkirkby commented 6 years ago

In other packages I have replaced basestring with six.string_types. Does that sound sufficiently future proof @weaverba137?

weaverba137 commented 6 years ago

six is a fine choice, and most astropy-affiliated packages already use it. Another way is to simply stop supporting Python 2. Astropy itself is doing that.

dkirkby commented 6 years ago

I would love to drop python 2, but I don't want to be the first one to break the DESI stack. Do we have a date for dropping python 2 support?

sbailey commented 6 years ago

There are corners of the DESI code that no longer support python 2 and we officially don't support it since the 18.6 release, though it does still work for much of the DESI code. If speclite imports (not just speclite usage) no longer worked with py2, that would effectively finish off our historical support of py2. That would be ok.

Alternatively, back when we actively supported py2, we had code like this to support running out of bare git clones without installation:

if sys.version_info[0] > 2:
    basestring = str
londumas commented 5 years ago

Just to say, although I bet you know, that this is still an issue. and replacing if isinstance(response, basestring): by if isinstance(response, str): do the job.

Traceback (most recent call last):
  File "<HOME>/Programs/desi//code//desisim/bin/quickquasars", line 9, in <module>
    sys.exit(main())
  File "<HOME>/Programs/desi/code/desisim/py/desisim/scripts/quickquasars.py", line 797, in main
    bal=bal,eboss=eboss)
  File "<HOME>/Programs/desi/code/desisim/py/desisim/scripts/quickquasars.py", line 459, in simulate_one_healpix
    noresample=True, seed=seed, south=issouth)
  File "<HOME>/Programs/desi/code/desisim/py/desisim/templates.py", line 2724, in make_templates
    nocolorcuts=nocolorcuts, noresample=noresample, south=south)
  File "<HOME>/Programs/desi/code/desisim/py/desisim/templates.py", line 2509, in _make_simqso_templates
    maggies = self.bassmzlswise.get_ab_maggies(flux, self.basewave.copy(), mask_invalid=True)
  File "<HOME>/Programs/dkirkby/speclite/speclite/filters.py", line 1703, in get_ab_maggies
    method=FilterResponse.get_ab_maggies)
  File "<HOME>/Programs/dkirkby/speclite/speclite/filters.py", line 1650, in _get_table
    data = method(r, spectrum, wavelength, axis)
  File "<HOME>/Programs/dkirkby/speclite/speclite/filters.py", line 1087, in get_ab_maggies
    interpolate=True, axis=axis, units=default_flux_unit)
  File "<HOME>/Programs/dkirkby/speclite/speclite/filters.py", line 1040, in convolve_with_array
    self, wavelength, photon_weighted, interpolate, units)
  File "<HOME>/Programs/dkirkby/speclite/speclite/filters.py", line 1308, in __init__
    if isinstance(response, basestring):
NameError: name 'basestring' is not defined
weaverba137 commented 4 years ago

This was apparently fixed by #46. Reopen if that's incorrect.