-1

Project structure is like this:

.
├── README.md
├── pyproject.toml
...
my_project_name/
    ├── moduleA.py
    ├── moduleB.py
    └── FolderC
        └── moduleC.py

moduleA.py:

from .moduleB import moduleB_func
from .FolderC.moduleC import moduleC_func
...

script in pyproject.toml:

funcA_run = my_project_name.moduleA:funcA

(funcA function is just example - exception appears on start of compiling at the second line on import)

If i type poetry run funcA_run in project directory (where my_project_name folder, readme.rst, pyproject.toml and other poetry files), moduleB imports successfully, but moduleC from subfolder is not:

ImportError: cannot import name 'moduleC_func' from 'FolderC' (unknown location)

Removing . from import doesn't help.

With import FolderC.moduleC.moduleC_func i getting

ModuleNotFoundError: No module named 'FolderC.moduleC_func'

If moduleC.py import would be in moduleB.py, same will happen.

So, because of this i can't run any scripts, that contains local relative imports from my project.

Poetry version is 1.1.14

EDIT: Problem as it shown in this question is unreproducable. More details in my answer.

  • What command do you run exactly and from what current working directory? – sinoroc Sep 22 '22 at 21:04
  • @sinoroc edited question, hope i answered – metramarta Sep 24 '22 at 18:31
  • Your `module*.py` files seems to be at the wrong directory level. When you start a Python package project with Poetry, you'll have a `my_project_name/` directory in base your project directory, where *all* your Python (package) files should reside. Not outside of that directory: that will mess up the search path. Given the location of `Readme.md`, the latter seems to be the case: the two `moduleA/B.py` files are outside that directory, which is incorrect. – 9769953 Sep 24 '22 at 18:35
  • @9769953 you get it wrong, maybe i just explained bad. All modules are in `my_project_name/`. edited question – metramarta Sep 25 '22 at 09:50
  • Slight correction to that: you had the incorrect folder structure in your initial question (look at the version history), where the readme was at the same level as the A and B modules. – 9769953 Sep 25 '22 at 12:06
  • I can't reproduce your problem. I've created your project structure (I even remove the `__init__.py` file), with the three functions, and each function simply printing "A", "B" or "C", and it works as expected. I also can't see something obviously wrong in the code in the question, so there is something else that you haven't shown that is causing this problem. I'm using the same Poetry version. – 9769953 Sep 25 '22 at 12:18
  • @9769953 ok that's strange. I also can't reproduce problem in project given as example in question. For now i have no idea why in my project this problem appears. There is some screenshots from my case (tg bot): [1](https://prnt.sc/In4jdpwdWfqY) [2](https://prnt.sc/MNwbD6Dgug1X) [3](https://prnt.sc/dc4WWAVQeaOp) (exception appears not only on MongoInterface import) Also i met this problem earlier in another scripts in this project. – metramarta Sep 25 '22 at 15:51
  • Your `mongo_db` import is not relative. Put a `.` in front of it first. Then also try `from bot.mongo_db...`, so it has the full package name. But the leading period should work. If that fails, I'd be happy to look at the exception you get in the case, because that is more relevant. The current import in the first screenshot is simply invalid. – 9769953 Sep 25 '22 at 18:21
  • @9769953 Forgot to return `.` back after attemps to resolve problem. If i add it, problem remains. [Exception i get](https://prnt.sc/ldeKG3N0mhtG) – metramarta Sep 26 '22 at 12:47
  • That last linked exception *still* has no leading `.`: I read `from mongo_db.mongo_interface import MongoInterface`. – 9769953 Sep 26 '22 at 13:28
  • oops. [link](https://prnt.sc/wHOgRmlJ6Dtj) – metramarta Sep 26 '22 at 14:48
  • Sigh... Yes, there is a leading period now; in `bot_run.py`. But look at the *last* line of code that causes the exception: that does *not* have a leading period, and *that's* the cause of your current exception; see the `mongo_interface.py` file (in fact, since it has go through the parent, it probably requires two periods; or use the full package name). – 9769953 Sep 27 '22 at 07:36
  • Please take the time to (carefully) read through the code, and the exceptions, and solve the problem yourself. Instead of capturing screenshots (which [is not a good thing](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors-when-asking-a-question/285557#285557), posting stuff on the internet and hoping that will fix things. This way, it's looking to be an attempt at using a millions monkeys to write Shakespeare. – 9769953 Sep 27 '22 at 07:40
  • Thank you. Actually, of course i tried to solve it myself. I met same problem before on earlier stages of project, and every time couldn't resolve it. I figured out how to correctly use relative imports in this case, and it works if i run script manually, but somewhy `poetry run` gives another result. Regarding screenshots of code - i know that it's kinda annoying, i'm not pretty experienced on stackoverflow - should i update question every time, or what? In comments i can't just pase all the code (and make it readable), so i decided to use screenshots. Anyway, thank you for trying to help. – metramarta Sep 28 '22 at 12:02
  • Also, in `mongo_interface.py` i changed local import format, and now `poetry run` runs it with no exceptions. But, with this changes i made now i can't run this script manually, with `python bot_run.py`. For `poetry run` all relative imports should be relative to file, that contains them, but for usual python compiler they should be relative to file, that imports file that contains them. So i can't use `poetry run script_name` and `python script.py` at the same time... – metramarta Sep 28 '22 at 12:21

1 Answers1

0

Problem is resolved in some sense. Somewhy, poetry run requires another format of imports.

With structure like this:

├── README.md
├── pyproject.toml
...
my_project_name/
    ├── moduleA.py
    └── FolderC
        └── moduleC.py
        └── moduleC_2.py

If moduleC.py imports something from moduleC_2.py, and i import moduleC.py in moduleA.py, imports should look different if two cases:

If i run moduleA.py as script with poetry run:

# moduleA.py:
from .FolderC import moduleC.py  # with .

# moduleC.py
from .moduleC import some_func  # relatively folderC and with .

If i run moduleA.py manually with python moduleA.py:

# moduleA.py:
from FolderC import moduleC.py  # without .

# moduleC.py
from folderC.moduleC import some_func  # relatively my_project/ and without .