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

Tuesday, August 23, 2022

[FIXED] What is the correct way to import this class?

 August 23, 2022     import, module, package, python     No comments   

Issue

I have the following repository structure:

directoryA
    - moduleA.py (it contains MyClass class)
directoryB
    - moduleB.py

In moduleB.py I need to import MyClass, I use the following command:

from directoryA.moduleA import MyClass

When I run the main() function in moduleB.py I have the following error:

ModuleNotFoundError: No module named 'directoryA'

I run the moduleB.py from directoryB in the following way:

py moduleB.py

How could I fix this problem?


Solution

The best you can do to organizing your scripts would be using a file called main.py in the main folder, which must be in the same directory as directoryA and directoryB.

In main.py, just import the files from child directories:

from directoryA.moduleA import MyClass
import directoryB.moduleB

# Do whatever you want, but sometimes, just don't go too overboard.


Answered By - Keyacom
Answer Checked By - Marilyn (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