Question: cannot import pysam. Module has no attribute error
0
gravatar for QVINTVS_FABIVS_MAXIMVS
3.6 years ago by
USA SoCal
QVINTVS_FABIVS_MAXIMVS2.2k wrote:

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 • 5.5k views
ADD COMMENTlink modified 5 months ago by RamRS20k • written 3.6 years ago by QVINTVS_FABIVS_MAXIMVS2.2k
2
gravatar for rtliu
3.6 years ago by
rtliu2.0k
New Zealand
rtliu2.0k wrote:

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

pip install pysam
ADD COMMENTlink modified 5 months ago by RamRS20k • written 3.6 years ago by rtliu2.0k
2
gravatar for duxan
3.2 years ago by
duxan50
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 5 months ago by RamRS20k • written 3.2 years ago by duxan50
2
gravatar for John
3.2 years ago by
John12k
Germany
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. http://docs.python-guide.org/en/latest/dev/virtualenvs/

ADD COMMENTlink modified 5 months ago by RamRS20k • written 3.2 years ago by John12k
1

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 REPLYlink modified 5 months ago by RamRS20k • written 3.2 years ago by duxan50

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

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

Help
Access

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