0

I am new to this packaging, and facing now a problem. The problem is as follows. I have created a poetry project from the CLI:

 poetry new mypackage_test

and have a file structure as follows:

enter image description here

here both my init.py are empty

pyproject.toml

[tool.poetry]
name = "mypackage-test"
version = "0.1.0"
description = ""
authors = ["my name <my@name.com>"]
readme = "README.md"
packages = [{include = "mypackage_test"}]

[tool.poetry.dependencies]
python = "^3.10"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

helloWorld.py

from pk1 import mypack


def helloworld():
    print("hello world")
    print(mypack.printMypk1())


if __name__ == "__main__":
    helloworld()

mypack.py

def printMypk1():
    print("in mypackage 1")

when I run this code eg.:

python helloWorld()

then it works as expected, but if i install this as a package and du this in python:

>>> from mypackage_test import helloWorld

i get this error:

Traceback (most recent call last): File "", line 1, in File "C:\Programs\Python\Python3102\lib\site-packages\mypackage_test\helloWorld.py", line 1, in from pk1 import mypack ModuleNotFoundError: No module named 'pk1'

am i doing this wrong? any suggestion ?

Note: if i unzip the ziped file in the dist folder then i can see that the files are packed/ziped as expected

sinoroc
  • 18,409
  • 2
  • 39
  • 70
KapaA
  • 165
  • 14
  • 1
    can you try from .pk1 import mypack, you need to add (.) – lackti Jan 20 '23 at 09:08
  • i get this: "ImportError: attempted relative import with no known parent package" – KapaA Jan 20 '23 at 09:11
  • I hope this link will help you, I think it's better idea to work with setup [https://stackoverflow.com/questions/54430694/python-setup-py-how-to-get-find-packages-to-identify-packages-in-subdirectori](https://stackoverflow.com/questions/54430694/python-setup-py-how-to-get-find-packages-to-identify-packages-in-subdirectori) – lackti Jan 20 '23 at 09:23
  • hmm getting the same error with setuptool... :( – KapaA Jan 20 '23 at 09:46
  • 1
    Use absolute imports, always. In `mypackage_test/helloWorld.py`: `from mypackage_test.pk1 import mypack`. – sinoroc Jan 20 '23 at 11:28

1 Answers1

1

Use absolute imports, always. In mypackage_test/helloWorld.py: from mypackage_test.pk1 import mypack. – sinoroc

thank you @sinoroc this helped

KapaA
  • 165
  • 14