Question: cannot import pysam. Module has no attribute error
5.2 years ago by

If I do this

$ python -c "import pysam"

I get no errors

However, if I do this

import pysam
bam_file = pysam.AlignmentFile("in.bam", "rb")

# ...

I get this error

AttributeError: 'module' object has no attribute 'AlignmentFile'

and this ...

$ conda update pysam
Fetching package metadata: ....
# All requested packages already installed.
# packages in environment at /home/xxx/anaconda:
pysam                     0.6                      py27_0


What's this mojo

pysam python samtools api • 7.2k views
ADD COMMENTlink modified 2.1 years ago by RamRS30k • written 5.2 years ago by QVINTVS_FABIVS_MAXIMVS2.4k
gravatar for rtliu
5.2 years ago by
New Zealand
rtliu2.1k wrote:

It seems your pysam version 0.6 is not up-to-date, try the latest version:

pip install pysam
ADD COMMENTlink modified 11 months ago by RamRS30k • written 5.2 years ago by rtliu2.1k
gravatar for duxan
4.8 years ago by
Serbia/Novi Sad
duxan50 wrote:

As @rtliu suggested, you should install latest version of pysam, which does have AlignmentFile in API to samtools (from version 0.8.1). To do that inside conda environment, you should first activate the environment you are using and install pysam as non-conda package, through pip. From the DOCS:

source activate myenv
pip install pysam

NOTE: Since you already have pysam 0.6, you'll probably need to upgrade version instead of clean install, so 2nd command should be pip install pysam --upgrade

ADD COMMENTlink modified 2.1 years ago by RamRS30k • written 4.8 years ago by duxan50
gravatar for John
4.8 years ago by
John12k wrote:

The latest version of pysam for anaconda is 0.6 (which you would install with conda install pysam as that links everything up properly), but 0.6 doesn't use the new module names (also, why are we changing module names in the first place guys!? Aliases are cheap.)

To get the latest version of pysam, you'll need to do as duxan says, which is to install pysam via pip - but not the system pip as rtliu's example shows, but the pip that is used by anaconda. This can be done either as duxan suggests, or, a slightly easier way is to just find and use the pip that anaconda uses (/some_location/anaconda/bin/pip install pysam --upgrade) without the need to source the virtual environment.

However, this is not the "Anaconda way". What you are doing now is making an up-to-date python environment, which is sort of the opposite of what Anaconda is supposed to be. It is supposed to be a regulated, reliable and stable environment, where everything works together hassle-free. If something isn't hassle-free, you are supposed to either whine about it to the Anaconda devs, or stop doing what you are doing.

In short, if you are going to install your own packages manually like this, you are much much much better off ditching anaconda entirely and learning how to make, configure, and delete virtual environments for python.

ADD COMMENTlink modified 2.1 years ago by RamRS30k • written 4.8 years ago by John12k

Good point about aliases, especially since it seems nothing else changed dramatically in pysam/samtools.

As for managing packages with anaconda - another way to add a package into conda env would be to search for different versions of it into public channels. To install: conda install -c<channel_name> <package_name>

ADD REPLYlink modified 2.1 years ago by RamRS30k • written 4.8 years ago by duxan50

I didn't know you could do that - thanks duxan :D

ADD REPLYlink written 4.8 years ago by John12k
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1696 users visited in the last hour