PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 25, 2022

[FIXED] Why am I getting ModuleNotFoundError?

 August 25, 2022     import, module, python, python-3.x, python-import     No comments   

Issue

Here is the directory and file as a tree:

.
├── my_dir
│   ├── a.py
│   └── b.py
└── outside.py

a.py has only one function:

def my_func_a():
    return "This is a test from a"

In b.py I have imported the a.py module and used its function inside another function:

from a import my_func_a
    
def my_func_b():
    print(my_func_a())
    return "This is a test from b"

Now, in outside.py, when I am running the code, I am facing the ModuleNotFoundError:

from my_dir import b

print(b.my_func_b())

the Error:

from a import my_func_a
ModuleNotFoundError: No module named 'a'

Solution

The importing should be as follows and it is working perfectly!

from .a import my_func_a


Answered By - Dolan
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing