Import a package but disable importing the relative parent

11 Views Asked by At

Here is my file system

main.py
setup.py
scripts/
    __init__.py
    scriptA.py
src/
    myapp/
        __init__.py
        utils1/
            utils1foo.py
            utils1bar.py
        utils2/
            utils2foo.py
            utils2bar.py

I'm trying to setup my code in a way where main.py would call import myapp this way

import myapp
FOO_1 = myapp.utils1.utils1foo.FOO_1
BAR_2 = myapp.utils2.utils2bar.BAR_2
hello = myapp.hello() # Defined in myapp.__init__.py

import scripts
world = scripts.scriptA.world()

I have a working solution at the moment which is

setup.py

import setuptools
setuptools.setup(
    ...
    packages=['myapp', 'scripts'],
    package_dir={"": "src", "scripts": "."}
)

However, this also allows me to type import src or any other relative imports from there import src.myapp.{etc}

I only want the user to be able to import myapp and not the src/ containing it. How do I tell setuptools to package it this way ?

I tried setting the packages_dir argument with "myapp": "src" but then I couldn't import myapp anymore

0

There are 0 best solutions below