scarrazza / apfel

A PDF Evolution Library
http://apfel.mi.infn.it/
GNU General Public License v3.0
12 stars 9 forks source link

can not import in Python #34

Closed zhyiyu closed 3 years ago

zhyiyu commented 3 years ago

I installed APFEL and tried to use it in Python

import apfel

and see the following error

ModuleNotFoundError: No module named 'apfel'

However, import in C++ has no issues

#include "APFEL/APFEL.h"

Could you please point out to me what I should do to make APFEL importable in Python? Thanks!

scarrazza commented 3 years ago

This most likely means that the python module was compiled and installed in a different python environment. You can check the python version and module location in the installation output and make sure it matches your current python interpret and environment (including PYTHONPATH).

alecandido commented 3 years ago

Moreover: default python on systems used to be python2, but most likely you want to use python3, so you need to to fix one line in configure.ac: https://github.com/scarrazza/apfel/blob/df064ef35050472b39e31e9e1f0ac731defb5bdb/configure.ac#L99 adding parenthesis for the print function (before was the print statement), then reconfigure (to regenerate the corrected configure script):

autoreconf -f -i

and specify it at install time

PYTHON=$(which python3) ./configure
zhyiyu commented 3 years ago

@AleCandido Thank you for the comment, I have modified line 88 of apfel/configure.ac such that it now looks like

python_incpath=`$PYTHON -c "import distutils.sysconfig; print(distutils.sysconfig.get_python_inc();)"`

and run autoreconf -f -i, which I think gave message of success (no errors). However, when I ran

PYTHON=$(which python3) ./configure --prefix=/path/to/destination

I got error

Illegal variable name.

Is there anything I did wrong?

Thanks!


By the way, when I echo $PYTHON, I get PYTHON: Undefined variable..

alecandido commented 3 years ago
print(distutils.sysconfig.get_python_inc();)"`

Closed parenthesis is in the wrong position:

print(distutils.sysconfig.get_python_inc());"`

By the way, when I echo $PYTHON, I get PYTHON: Undefined variable..

This is expected, when you put at the beginning of the command you only set for that one, but it does not persist in your environment. If you want to keep it you can simply issue a command before with export PYTHON=$(which python3) (export is needed to make it available to subshells).

zhyiyu commented 3 years ago

@AleCandido Thank you, I have fixed the parenthesis and defined $PYTHON. However, after the installation, I still can not import apfel in Python 3.

And I checked config.log as suggested by @scarrazza, I think it is using indeed the correct Python 3 path I have

configure:20263: checking for python
configure:20293: result: /w/jam-sciwork18//zyy/packages/anaconda_3/bin/python3

Is there something else that I can check? Thanks!

alecandido commented 3 years ago

Then do the thing in two steps as said before:

  1. export PYTHON=$(which python3)
  2. ./configure etc. (i.e. make and make install)

At this point you can check what PYTHON3 is, by:

echo $PYTHON3

the apfel package should then be available from that interpreter (that in the first place is the interpreter that you accessed with which python3, and so the one you enter when you enter python3 as a command).

Actually, when you enter make install it should copy the stuffs around, and it's printing where, e.g.:

copying build/lib.linux-x86_64-3.8/_apfel.cpython-38-x86_64-linux-gnu.so -> /home/alessandro/Projects/N3PDF/external/apfel/env/lib/python3.8/site-packages

you could then try to grep during make install:

make install | grep "copying"

to see where the .so has been installed.

zhyiyu commented 3 years ago

I found out where the .so file is moved to

copying build/lib.linux-x86_64-3.7/_apfel.cpython-37m-x86_64-linux-gnu.so -> /w/jam-sciwork18/zyy/packages/apfel/lib/python3.7/site-packages

Hence I do

import sys
sys.path.append('/w/jam-sciwork18/zyy/packages/apfel/lib/python3.7/site-packages')
import apfel

and it works! Thank you for your help! Hope this becomes easier in the future!


By the way, is it still necessary to define environmental variables PYTHON and PYTHON3?

alecandido commented 3 years ago

By the way, is it still necessary to define environmental variables PYTHON and PYTHON3?

Nope: the PYTHON variable is only required during the run of configure, that's why the first time I told you to put it inline. Later on I told you to export only for debug purpose. PYTHON3 variable should be completely unused, so it's useless to define it (and maybe confusing).


and it works! Thank you for your help! Hope this becomes easier in the future!

I agree that it's not as easy as pip install apfel, but should not even be that complicated: somehow you are installing in:

/w/jam-sciwork18/zyy/packages/apfel/

this is the prefix that is determined, and there will be most likely a python executable there, but instead you are using another one, i.e.:

/w/jam-sciwork18//zyy/packages/anaconda_3/bin/python3

So:

  1. if you use enter /w/jam-sciwork18/zyy/packages/apfel/bin/python (or /w/jam-sciwork18/zyy/packages/apfel/bin/python3 /w/jam-sciwork18/zyy/packages/apfel/bin/python3.7) as a command, then you should be able there to directly import apfel, without the need of changing your path
  2. if you want to use it from your anaconda environment you should be able to install there, specifying that one as PYTHON during configure, and changing prefix, so running:
    PYTHON=/w/jam-sciwork18//zyy/packages/anaconda_3/bin/python3 ./configure --prefix=/w/jam-sciwork18//zyy/packages/anaconda_3/
alecandido commented 3 years ago

Moreover consider that:

  1. NNPDF is a using a pre-built conda package
  2. At some point (hopefully not so far in the future) apfel will dropped both as the provider of evolution and dis predictions, replaced by pure python packages (that will be deployed on PyPI)
zhyiyu commented 3 years ago

I see, so by

if you want to use it from your anaconda environment you should be able to install there, specifying that one as PYTHON during configure, and changing prefix, so running:

actually you mean that APFEL is supposed to be installed within anaconda if I would like to import it from Python. But maybe I should put it within

/w/jam-sciwork18//zyy/packages/anaconda_3/pkgs/