0

I am importing a package which has following folder structure:

package
├── <src>/
│   ├── p1/
│   │   └── p1.py
│   └── p2.py
└──scripts
     └──xyz.py

I want to import the xyz from the scripts folder in another package. How can I do it?

I can import the p1.py using from package.p1 import p1 but when I try to do from package.scripts import xyz I get the error ModuleNotFoundError: No module named package.scripts

Thanks for the help.

ani15
  • 1
  • 3
  • Hi, it depends where your root folder is, if this is the package, which doesn't seem the case, from scripts import xyz should be enough. Try to set your root folder in your IDE to package if it isn't and try again. – baskettaz Feb 27 '23 at 22:30
  • @baskettaz, I want to use this package from another package. – ani15 Feb 27 '23 at 22:38
  • 1
    Did you **install** this package? How exactly is the `package.p1` import working already, given that there is an intervening `src` folder? For example, did you use `setup.py` or `pyproject.toml` in order to explain where the packages are? – Karl Knechtel Feb 27 '23 at 22:45
  • do you mean package in the python sense(__init__ is missing if so) or folder? If all of this packages and modules aren't installed as Karl has written, you should put them in your directory and navigate diligently, taking your root folder in consideration. – baskettaz Feb 27 '23 at 22:51
  • @KarlKnechtel I have pip installed this package, and I am able to import all the classes in the `src` folder but not from the scripts folder. I am new to python so sorry for saying it package. Also the scripts folder does not have the init. – ani15 Feb 27 '23 at 22:59
  • There was nothing wrong with the way you used the word "package". The problem is that, if you want the scripts folder to be usable in **other projects** after you install your code, then it **also** needs to be installed as a package (or sub-package). Therefore, it is likely that this is **not actually** a problem about `import` syntax, but a question about **how to make the code properly installable**. Therefore we have a completely different situation, and need completely different information. – Karl Knechtel Feb 28 '23 at 01:21
  • "Also the scripts folder does not have the init." This almost certainly does not matter. **`__init__.py` does almost nothing**, by itself, in modern versions of Python; it's mainly needed for the code that would be written in that file. – Karl Knechtel Feb 28 '23 at 01:22

0 Answers0