Newbie package question

I’m trying to create my very first python package. However, I’m not sure what I’m doing wrong.

I created a subfolder “pack” containing 3 files:
(empty file)

def say_hello():
   print( 'hello world' )

import printer

def run():

Then in the root folder, I have a file that tries to use the package:

from pack import main

When I run, I get an error:

ModuleNotFoundError: No module named 'printer'

I think that in the import should be something like:

from . import printer  # relative import


from pack import printer  # absolute import
1 Like

Neither of these solutions work, however.

If I use:

from . import printer

Then when I run, it fails with:

ImportError: cannot import name ‘printer’ from ‘main’ (

And if I use “from pack import printer” I get:

ModuleNotFoundError: No module named ‘pack’

So I’m not sure how to set things up so that I can achieve two things:
(1) run the package from within itself
(2) run the package while importing it

This seems odd because I need the package to be able to run itself (to do self testing, etc.) but I still want to be able to import it into a larger project.

I am a newbie also and I have the same issue. The only way I can get this to work is to have read something like the below. I have no sense as to whether this is good practice or not though - I’m having a really hard time understanding how to organize modules into packages.

def run():

if __name__ == '__main__':
    import printer
    from . import printer

The simplest way to organise your package is like this:

+--  subpackage/

Of course you can have more than one sub-module or sub-package.

Technically it is not absolutely necessary to have an
file in the package, but the rules for skipping it are very complicated
and most people (including me!) don’t understand it. So save yourself a
lot of pain and always include, even if it is an empty

The file is used when you run the package as if it were a

# Don't do this!

# Do this instead:
python3 -m mypackage

# or if you have to specify the full path:
python3 path/to/mypackage/

That will automatically treat the package as a script and run the code
in the file. That keeps it nicely independent from the
code that gets run when you import the package:

import mypackage  # uses mypackage/

Of course you can import code inside like this:

from . import stuff

Try reorganising your package to put the script-like code in a seperate file and see if that helps.

1 Like

Thank you! I’m not sure I fully get it but it seems to work so far.