Search code examples
pythonimportmaya

python import module fails


custom/
    scripts/
    __init__.py
    file1.py
    utils/
        __init__.py
        utilFile1.py
        utilFile2.py
        utilFile3.py

I have the above file structure I'm struggling trying to figure out how to import modules in python. I have gone through stackoverflow posts that have the same question but I still couldn't figure out why I can't get it to work.

I am trying to import these modules in maya and when I run from utils import utilFile1 I get ImportError: cannot import name utilFile1. Running from custom.scripts.utils import utilFile1 gives me this error ImportError: no module named custom.scripts.utils. However if I run import file1 it imports without any errors

I have appended custom/scripts to sys.path and when that didn't work, tried appending custom/scripts/utils as well, but that didn't work either. Based off some of the posts on stackoverflow, I saw some people suggesting to run "python -m" but I'm not sure if I should run this or where to execute it from.

I'm really at a loss as to why I can't get it to work at all.


Solution

  • you have make it a python package by following steps,

    in your root dir create setup.py, in your case

    custom/
        setup.py
           scripts/
           ...
           ...
    

    in setup.py

    from setuptools import setup, find_packages
    
    setup(
        name='your_package_name',
        description='bla bla bla',
        version='0.0.1-dev',
        install_requires=[
    
        ],
    )
    

    and then hit

    pip install -e .
    

    for more info refer to this docs