Python glmnet “No module named _glmnet”

穿精又带淫゛_ 提交于 2019-12-24 05:03:40

问题


UPDATE Getting close. Now I'm running f2py on the .pyf file that should generate the _glmnet module.

I build the package python-glmnet packet with the following command.

python setup.py config_fc --fcompiler=gnu95         
                                           --f77flags='-fdefault-real-8'         
                                          --f90flags='-fdefault-real-8' build

But when I import the module I get this error:

File "/Users/rose/221/tagger/tagger/glmnet/glmnet.py", line 2, in import _glmnet ImportError: No module named _glmnet

How can I import that module?

The glmnet directory also contains a glmnet.pyf file that begins with the following:

!    -*- f90 -*-
! Note: the context of this file is case sensitive.

python module _glmnet ! in
    interface  ! in :_glmnet
        subroutine elnet(ka,parm,no,ni,x,y,w,jd,vp,ne,nx,nlam,flmin,ulam,thr,isd,lmu,a0,ca,ia,nin,rsq,alm,nlp,jerr) ! in :glmnet:glmnet.f
            integer optional :: ka=1 ! Use covariance updates over naive by default
            real*8 :: parm
            integer intent(hide),check(shape(x,0)==no),depend(x) :: no=shape(x,0)
            integer intent(hide),check(shape(x,1)==ni),depend(x) :: ni=shape(x,1)
            real*8 dimension(no,ni) :: x
            real*8 dimension(no),depend(no) :: y
            real*8 dimension(no),depend(no) :: w
            integer dimension(*) :: jd
            real*8 dimension(ni),depend(ni) :: vp
            integer optional,depend(x) :: ne=min(shape(x,1), nx)
            integer :: nx
            integer optional,check((flmin < 1.0 || len(ulam)==nlam)),depend(flmin,ulam) :: nlam=len(ulam)
            real*8 :: flmin
            real*8 dimension(nlam) :: ulam
            real*8 :: thr
            integer optional :: isd=1 ! Standardize predictors by default

UPDATE

Where can I find this _glmnet module? Is it contained in the glmnet.pyf file, as shown below? I tried adding this glment folder to my PYTHONPATH, but that didn't change anything.

~/221/tagger/tagger/glmnet master ls
__init__.py                  example_lasso_elastic_net.py glmnet.pyc
__init__.pyc                 glmnet.f                     glmnet.pyf
elastic_net.py               glmnet.py
~/221/tagger/tagger/glmnet master head -10 glmnet.pyf
!    -*- f90 -*-
! Note: the context of this file is case sensitive.

python module _glmnet ! in
    interface  ! in :_glmnet
        subroutine elnet(ka,parm,no,ni,x,y,w,jd,vp,ne,nx,nlam,flmin,ulam,thr,isd,lmu,a0,ca,ia,nin,rsq,alm,nlp,jerr) ! in :glmnet:glmnet.f
            integer optional :: ka=1 ! Use covariance updates over naive by default
            real*8 :: parm
            integer intent(hide),check(shape(x,0)==no),depend(x) :: no=shape(x,0)
            integer intent(hide),check(shape(x,1)==ni),depend(x) :: ni=shape(x,1)
~/221/tagger/tagger/glmnet master echo $PYTHONPATH
/Users/rose/221/tagger/tagger/glmnet:
~/221/tagger/tagger/glmnet master cd ..
~/221/tagger/tagger master python main.py
Traceback (most recent call last):
  File "main.py", line 14, in <module>
    from glmnet import glmnet
  File "/Users/rose/221/tagger/tagger/glmnet/glmnet.py", line 2, in <module>
    import _glmnet
ImportError: No module named _glmnet

回答1:


As far as I can tell from the source, it's looking for the _gmlnet module, which is defined in gmlnet.pyf. gmlnet.pyf is not a python module, it's a set of additional instructions for a program called f2py, and python will ignore the .pyf file. You need to compile the .pyf file along with a fortran file using f2py. Use a command like this:

f2py -c --fcompiler=gnu95 gmlnet.pyf gmlnet.f

Try installing f2py and then reinstalling the gmlnet package.




回答2:


Without knowing too much about what else is going on, this looks like python doesn't know where the _gmlnet package is located. If you haven't already, check the contents of your PYTHONPATH environment variable with echo $PYTHONPATH. If the _gmlnet directory isn't in there, you need to add it.

  • To add something to PYTHONPATH in bash, run the following command:

    export PYTHONPATH=/path/to/_gmlnet/directory:$PYTHONPATH

  • You can check that python has parsed this correctly by going into the interactive terminal and typing

    import sys

    print sys.path

  • Lastly, you likely don't want to have to type this into your shell each time you start up your terminal. For Mac OS, add the following to the end of your ~/.bash_profile file if you use bash. Make sure that this file ends with a newline.

    export PYTHONPATH=/path/to/_gmlnet/directory:$PYTHONPATH



来源:https://stackoverflow.com/questions/20459390/python-glmnet-no-module-named-glmnet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!