python import module fails

python import module from directory
python cannot import module
python import package
python no module named
python import module from path
python import example
python cannot find module
python cannot import name
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.

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

Traps for the Unwary in Python's Import System, While Python 3.3+ is able to import the submodule without any problems: due to broken imports (either failing to find example for absolute imports like import  Hello, I've looked at these issues already: #278 and #219 but I keep having an issue when trying to import a local module into a Python function. I'm using Python 3.7 and not 3.6 so it could be the reason why I can't get this to work.

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

5. The import system, If any of the intermediate imports fail, a ModuleNotFoundError is raised. 5.3.1. The module cache¶. The first place checked during import search is sys.modules . The import statement allows you to import one or more modules into your Python program, letting you make use of the definitions constructed in those modules. Using from … import. To refer to items from a module within your program’s namespace, you can use the from … import statement. When you import modules this way, you can refer to the functions by name rather than through dot notation

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.

Python Exception Handling, with code samples showing how to deal with failed imports in Python will raise a ModuleNotFoundError in Python 3.6 and newer versions. The import system¶ Python code in one module gains access to the code in another module by the process of importing it. The import statement is the most common way of invoking the import machinery, but it is not the only way. Functions such as importlib.import_module() and built-in __import__() can also be used to invoke the import machinery.

import python modules failedwhat am I missing?, Hello guys, I have a question about importing my own python modules from a package I wrote. I am using ROS Indigo on Ubuntu 14.04. If the script name refers directly to a Python file, the directory containing that file is added to the start of sys.path, and the file is executed as the main module. Source: Python 2 and 3. Let’s recap the order in which Python searches for modules to import: built-in modules from the Python Standard Library (e.g. sys, math)

Debugging with sys.path / ImportError issues, from foo.bar import baz. Python will start by looking for a module named foo, and then inside that a module named bar, and then inside that for an object named  pip is the easiest option, but that is not a Python command. pip is an executable which you can find in <Python installation directory>\Scripts. So to install requests, open Windows's Command Prompt (so not Python) and run: C:\Python35\Scripts\pip install requests (assuming C:\Python35 is the Python installation directory)

Importing custom python module fails - MATLAB Answers, path, this directory is included in the search path, however, importing the package fails ("Import argument 'py.fabio' cannot be found or cannot be imported.​"). I  If the loader inserted a module and the load fails, it must be removed by the loader from sys.modules; modules already in sys.modules before the loader began execution should be left alone (see importlib.util.module_for_loader()). The loader should set several attributes on the module.

Comments
  • Where are trying to import utilFile1.py?
  • From file1.py using from utils import utilFile1
  • Have you checked utilFile1.py for circular imports?
  • 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
  • can I run this on windows 10?
  • @dege yes you can