0
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.

4
  • Where are trying to import utilFile1.py? Commented Sep 21, 2018 at 4:56
  • From file1.py using from utils import utilFile1 Commented Sep 21, 2018 at 4:59
  • Have you checked utilFile1.py for circular imports? Commented Sep 21, 2018 at 5:06
  • I'm not importing anything from file1 in utilFile1. file1 is importing from files in utils but it's failing on the 1st file I'm trying to import and none of them are importing anything from file1 Commented Sep 21, 2018 at 5:15

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

can I run this on windows 10?
@dege yes you can
0

Please update the directory structure as below. Python might not be considering custom as a module as it doesn't have __init__.py file,

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

Comments

0

Yash's method works, I believe that's what companies use to setup their environments. An alternative way is to add your scripts path as a PYTHONPATH in your maya.env file instead of using sys.path.append. I'm not sure why there's a difference or if it's coz I'm trying this on windows, but oddly enough it worked in my case.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.