metaseq icon indicating copy to clipboard operation
metaseq copied to clipboard

py3: relative imports fail

Open endrebak opened this issue 8 years ago • 8 comments

(Conda gives a conflicting requirements error, so I installed metaseq with pip.)

When trying to import metaseq, I get the following error:

In [2]: import metaseq
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-2-3365faa9d779> in <module>()
----> 1 import metaseq

/local/home/endrebak/anaconda3/lib/python3.5/site-packages/metaseq/__init__.py in <module>()
      2 import sys
      3 import time
----> 4 import helpers
      5 from helpers import data_dir, example_filename
      6 from _genomic_signal import genomic_signal

ImportError: No module named 'helpers'

endrebak avatar May 24 '16 08:05 endrebak

If I am reading your Travis build log correctly, you only test on py27. Is this a requirement? If so, I guess I cannot use metaseq with snakemake as easily as I hoped. And you should add the requirement to your docs/README.md.

Pretend edit: tested conda install metaseq-all on py27 and it imports just fine.

endrebak avatar May 24 '16 08:05 endrebak

Relative imports were removed in py3 (good choice, imo), and this is why it fails. I fixed your metaseq/__init__.py like so:

import os
import sys
import time
from metaseq import helpers
from metaseq.helpers import data_dir, example_filename
from metaseq._genomic_signal import genomic_signal
import metaseq.plotutils
import integration
import integration.chipseq
import metaseq.colormap_adjust
import metaseq.results_table
import metaseq.tableprinter
from metaseq.version import __version__
import metaseq.persistence

but then I got this error:

In [1]: import metaseq
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-3365faa9d779> in <module>()
----> 1 import metaseq

/local/home/endrebak/anaconda3/lib/python3.5/site-packages/metaseq-0.5.6-py3.5.egg/metaseq/__init__.py in <module>()
      4 from metaseq import helpers
      5 from metaseq.helpers import data_dir, example_filename
----> 6 from metaseq._genomic_signal import genomic_signal
      7 import metaseq.plotutils
      8 import integration

/local/home/endrebak/anaconda3/lib/python3.5/site-packages/metaseq-0.5.6-py3.5.egg/metaseq/_genomic_signal.py in <module>()
     30
     31 import numpy as np
---> 32 from bx.bbi.bigwig_file import BigWigFile
     33
     34 import pybedtools

bx/bbi/bigwig_file.pyx in init bx.bbi.bigwig_file (lib/bx/bbi/bigwig_file.c:7691)()

bx/bbi/bbi_file.pyx in init bx.bbi.bbi_file (lib/bx/bbi/bbi_file.c:12911)()

ImportError: No module named 'cStringIO'

which I do not have time to look up now, but I'd guess it has to do with the same import issue.

endrebak avatar May 24 '16 08:05 endrebak

Yep, due to the dependency on bx-python, which itself is only py27, I can't make metaseq py3-compatible.

Whenever I need to use it within snakemake, I end up writing a separate script and run it in a py27 env. Awkward? Yes.

I think the best solution here is to replace bx-python with the py3-compatible pybigwig. That will require a bit of work and lots more tests. It's already on my list though.

That said, depending on what you need to do, deepTools might be a better option than metaseq.

daler avatar May 24 '16 13:05 daler

bx-python has actually been python3 compatible for a year or so, IIRC. Perhaps you should try installing it again?

But do not put this high on your todo-list for my sake, I'll easily find a workaround.

endrebak avatar May 24 '16 13:05 endrebak

The error you reported is a common error in non-py3-compatible code and, judging from your paths, is coming from bx-python as installed under py35. I had also tried building it a few months ago for py3 in bioconda and didn't have any luck. Are you able to use the bx.bbi.bigwig_file module in py3?

daler avatar May 24 '16 13:05 daler

Sorry, probably not! I always think bx = intervaltree, clustertree because it is the only two modules I use.

On Tue, May 24, 2016 at 3:46 PM, Ryan Dale [email protected] wrote:

The error you reported is a common error in non-py3-compatible code and, judging from your paths, is coming from bx-python as installed under py35. I had also tried building it a few months ago for py3 in bioconda and didn't have any luck. Are you able to use the bx.bbi.bigwig_file module in py3?

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/daler/metaseq/issues/26#issuecomment-221274417

endrebak avatar May 24 '16 16:05 endrebak

When I learn more about bigwig, I'll try out https://bitbucket.org/mruffalo/bx-python3 and see if it works.

Perhaps it can be bundled with metaseq? It probably will not get more updates, but might be good enough.

endrebak avatar May 25 '16 05:05 endrebak

I think it might be more beneficial to port the original bx-python (which I believe is bxlab/bx-python) to Python3 rather than switching to an unmaintained fork.

@endrebak you could get Python3 behaviour in Python2 by importing

from __future__ import absolute_import

After this import the following

import metaseq.colormap_adjust
import metaseq.results_table
import metaseq.tableprinter

could be concisely written as

from . import colormap_adjust, results_table, tableprinter

superbobry avatar May 25 '16 16:05 superbobry