cannot import pysam. Module has no attribute error
3
0
Entering edit mode
9.4 years ago

If I do this

$ python -c "import pysam"

I get no errors

However, if I do this

#!/home/xxx/anaconda/bin/python
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

Works???

What's this mojo

pysam python samtools api • 11k views
ADD COMMENT
3
Entering edit mode
8.9 years ago
duxan ▴ 70

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 COMMENT
2
Entering edit mode
9.4 years ago
rtliu ★ 2.2k

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

pip install pysam
ADD COMMENT
2
Entering edit mode
8.9 years ago
John 13k

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. http://docs.python-guide.org/en/latest/dev/virtualenvs/

ADD COMMENT
1
Entering edit mode

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 https://conda.anaconda.org/<channel_name> <package_name>

ADD REPLY
0
Entering edit mode

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

ADD REPLY

Login before adding your answer.

Traffic: 2497 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6