fmagrini / seislib

Seismic imaging at local, regional, and global scale
https://seislib.readthedocs.io/en/latest/
MIT License
72 stars 19 forks source link

ModuleNotFoundError: No module named 'seislib' #5

Closed JiajunChong closed 1 year ago

JiajunChong commented 1 year ago

Dear Magrini, Thanks for sharing the nice software. I currently have a question about using seislib. The seislib has been installed successfully with "pip install seislib" following the instruction in the wiki, however, the module can't be found. I posted the way of installation and error message as following. Could you please help? Thanks!

(1) the way I install seislib: conda create -n seislib python=3.9 numpy=1.20 conda activate seislib conda install -c conda-forge obspy conda install -c conda-forge cartopy=0.18 conda install -c anaconda cython pip install seislib

Finally seislib is located in conda env: ./anaconda3/envs/seislib/lib/python3.9/site-packages

(2) Error message when trying to import the module

import seislib

ModuleNotFoundError Traceback (most recent call last) Cell In [3], line 1 ----> 1 import seislib

ModuleNotFoundError: No module named 'seislib'

fmagrini commented 1 year ago

Hello,

Unfortunately I need more info to understand what's going on. First of all, you will of course need to have a gcc compiler installed on your computer, otherwise the pip installation will (silently) fail because it will not be able to compile the cython part of the library. To install gcc on Ubuntu, see e.g. https://linuxize.com/post/how-to-install-gcc-compiler-on-ubuntu-18-04/

Assuming you already had ggc (if not, install it and then reinstall the seislib library), the commands you wrote above have first created an anaconda environment (named "seislib") and then installed all the dependences. This is correct. Now, if you open a new terminal and type

conda activate seislib python import seislib

You should indeed be able to import seislib.

If you instead want to use the Spyder GUI, rather than importing seislib through your terminal, make sure to have the package spyder-kernels installed on your seislib environment. In practice, open a new terminal and do as follows:

conda activate seislib pip uninstall seislib conda install -c conda-forge spyder-kernels pip install seislib

Note that we first uninstalled seislib and then resinstalled it: it is always good practice to uninstall your PyPi packages before installing a new package through conda. Having done so, we need the path to the python binary file associated with your seislib environment. Type in the same terminal (or in another, after activating seislib)

python import sys print(sys.executable)

Copy the output somewhere. It will be something like /home/username/anaconda3/envs/seislib/bin/python.

Now deactivate the environment through

exit() conda deactivate

(the first command closes Python) and open Spyder by typing in the terminal

spyder

Once you are within your Spyder GUI, go to Preferences > Python Interpreter. Click on "Use the following Python interpreter" and paste the path you have previously copied (e.g. /home/username/anaconda3/envs/seislib/bin/python). Now restart Spyder. After this, you should be able to import seislib correctly through Spyder too.

Hope this helps, Cheers

JiajunChong commented 1 year ago

Thanks Magrini! I'm using selisb with Jupyter-lab in WSL2, and the gcc compiler was installed before installing seislab. In Jupyter-lab, the python kernel points to /home/jiajun/anaconda3/envs/seislib/bin/python3 and the module can't be imported as shown previously.

If I run it with Python,

conda activate seislib python import seislib

It complains as following, seems the tomography part has not been installed correctly.

Python 3.9.15 (main, Nov 24 2022, 14:31:59) [GCC 11.2.0] :: Anaconda, Inc. on linux Type "help", "copyright", "credits" or "license" for more information.

import seislib Traceback (most recent call last): File "", line 1, in File "/home/jiajun/anaconda3/envs/seislib/lib/python3.9/site-packages/seislib/init.py", line 14, in from .tomography.grid import EqualAreaGrid File "/home/jiajun/anaconda3/envs/seislib/lib/python3.9/site-packages/seislib/tomography/init.py", line 27, in from .tomography import * File "/home/jiajun/anaconda3/envs/seislib/lib/python3.9/site-packages/seislib/tomography/tomography.py", line 70, in from seislib.tomography._ray_theory._tomography import _compile_coefficients ModuleNotFoundError: No module named 'seislib.tomography._ray_theory._tomography'

fmagrini commented 1 year ago

Good. You should be able to solve this problem through the procedure explained here https://github.com/fmagrini/seislib/issues/4

In the worst-case scenario, where the manual compilation of the Cython code will not work, you should get a more explicit error pointing you to the real problem. Just remember to activate your seislib environment before calling

python setup_all.py build_ext --inplace

Please, if you manage to solve the above problem, post here the solution and close this thread.

Thanks!

JiajunChong commented 1 year ago

Hi Magrini, Thanks a lot! I solved the problem as you explained in #4 Just as following: pip uninstall seislib sudo apt update sudo apt install build-essential sudo apt-get install manpages-dev pip install seislib