0

I have a Python library which works in a specific field, created to simulate several astronomical behaviours. This library it was made by my teacher, and now I've made another module with a new class inside. Is it necessary to do something to install this new module? I use Jupyter Notebook to run my scripts and I used spyder to make it. I'm new with programming and I need help.

I tried to bring this module inside library directory, where are all modules that I usually use, and so I import class inside it in a Jupyter Notebook as other classes. For example:

from modul_A import class_A

from myModul import myClass

modul_A and myModul are in the same directory, but first import work and second import doesn't work. Can you help me?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • You'll need to share more actual information to get help sorting this. You say the second import doesn't work; however, you don't tell us *how* it doesn't work? Gives an error or fails silently? What does the file name for `modul_A` vs `myModul` look like? Have you restarted the kernel and tried again? (By the way, a minor aside is that packages are suggested to have lowercase names, see [here](https://peps.python.org/pep-0008/#package-and-module-names).) Are you able to make a simple script and import a function from it? – Wayne Apr 08 '22 at 16:29
  • With more precision, I've several file .py in a directory called `/home/gabriele/Tesi/software/pyLensLib-1.0/pyLensLib-1.1/pyLensLib`, which are full of classes which make a lot of actions on astronomical images. For example, in my Jupyter Notebook I import a class with this mark: `from pyLensLib.sersic import sersic` ; where sersic.py is in the directory written above, and sersic is a class inside it. So, I've written a module magmeasure.py and I brought it in the same directory. But when I import with: `from pyLensLib.magmeasure import magnification_measures` ; It show an error. – Gabriele Troviso Apr 13 '22 at 13:26
  • The error is: `ModuleNotFoundError: No module named 'pyLensLib.magmeasure' ` – Gabriele Troviso Apr 13 '22 at 13:30
  • You probably want to look into Python packaging information. I suspect in your `/home/gabriele/Tesi/software/pyLensLib-1.0/pyLensLib-1.1/pyLensLib` directory (or the one above it?) there is a file called `__init__.py`? Does it have code in it that includes anything about `sersic`? If so, you probably can model import of your class after it and then maybe it will work? – Wayne Apr 13 '22 at 16:47
  • Also, since you said originally you are new with programming, I'm going to point out a concern. I see `pyLensLib-1.0/pyLensLib-1.1` in your path? It looks like version 1.1 is placed inside a directory for 1.0? I would prefer these be separate directories on the same level so there's no chance you are importing something that's importing something from version 1.0 still. But maybe that is how your teacher did it and you have to live with it? A suggestion is make sure you can get your classes to import when they are in a simple python script alongside your notebook. Maybe you already did that? – Wayne Apr 13 '22 at 16:59
  • I start from your last answer. I've never understood why there is this kind of path, as you said my teacher sent me this package and I didn't modify it to avoid errors or problems. About first answer, there is __init__.py and inside there are also "from .sersic import *". I tried to add "from .magmeasure import *" but don't work. When I write: "from pyLensLib import magmeasure" , I see `cannot import name 'magmeasure' from 'pyLensLib' (/home/gabriele/anaconda3/lib/python3.7/pyLensLib/__init__.py)` , and if I write "from pyLensLib.magmeasure import..." I have same problem written above. – Gabriele Troviso Apr 15 '22 at 10:26
  • Getting `__init__.py` to match your new import needs would seem critical given you can see how `sersic` is coded for import in there. So I think you are heading the right direction there. Why making the leap to yours failed isn't obvious. I would suggest starting smaller though & see where it breaks. Copy `sersic.py` that you see & change the name of that file to `serrsic.py`. Copy the line in the `init` file that is `from .sersic import *` and edit the new line to `from .serrsic import *`. Inside `serrsic.py` change spelling on one of the classes or functions & make sure you can import that. – Wayne Apr 15 '22 at 16:34
  • That hopefully will work to import, and if it does then you can start adding some of your code blocks in and see what breaks. If you can import your class and functions from in there, then try changing the file names. (Hopefully you reloading your console each time after you make a change in the library files and then trying import. Once, active Python imports from a source, it won't reimport unless forced because it doesn't assume the files changed.) – Wayne Apr 15 '22 at 16:37
  • I forgot you are working in the notebook. So you need to restart the kernel or force reload, see [here](https://stackoverflow.com/a/54440220/8508004). The flipside to trying to get an altered `serrsic.py` and related stuff imported, is something I already suggested you do. You don't say wether you can import your own functions or classes from a simply `.py` file in the directory alongside your notebook file? – Wayne Apr 15 '22 at 16:46
  • 1
    Just came across this [nice example of simple import](https://stackoverflow.com/a/71947492/8508004) spelled out, which was sort of what I was suggesting trying in parallel with some of your code. That way you can at least test your code works while developing and then integrate with your teacher's code better later. – Wayne Apr 21 '22 at 17:48

0 Answers0