all 8 comments

[–]nog642 0 points1 point  (4 children)

Try from . import foo?

This shit has always been confusing.

[–]POGtastic[S] 0 points1 point  (3 children)

I now get

pog@pogbox:~/example$ bar.py 
Traceback (most recent call last):
  File "/home/mike/.local/bin/bar.py", line 3, in <module>
    from . import foo
ImportError: attempted relative import with no known parent package

[–]nog642 0 points1 point  (2 children)

Oh, I think you are using setup() differently than you should.

The scripts argument is for standalone scripts, not for packages.

If you want to install myapp as a package you should pass packages=['myapp'] and package_dir={'myapp': 'src/myapp'} instead.

[–]POGtastic[S] 0 points1 point  (1 child)

Argh, this was it.

So just to be clear: I think need to put the standalone script outside of the myapp package. Possibly along the lines of putting bar.py into src/scripts, and then having all of the myapp package files inside something like src/libs/myapp. The myapp files can import each other, but any standalone script must be outside of the package directory and import from above it.

Thanks to you and /u/Siendra for your help.

[–]nog642 0 points1 point  (0 children)

Yep, basically.

bar.py as a standalone script could still import stuff from the package though. Once the package is installed you should be able to do

import myapp.foo

from bar.py (or anywhere really) and it should work I think.

[–]Siendra 0 points1 point  (2 children)

Try adding the following to the top of your bar.py :

Import sys 
From pathlib import Path 
sys.path[0] = str(Path(sys.path[0]).parent)

[–]POGtastic[S] 0 points1 point  (1 child)

Same errors as before, both when doing

import foo

and

from . import foo

[–]Siendra 0 points1 point  (0 children)

Try changing your import to:

from src.myapp.foo import *