"ModuleNotFoundError" when importing my own classes - python-3.x

I have 2 files, both are in the same folder.
The file that I am trying to import is "HelloWorldClass.py"
(Code to HelloWorldClass.py)
class HelloWorld():
def __init__(self):
print("Hello World")
and the file that I am calling "HelloWorldClass" from is "ClassTest.py"
(Code to ClassTest.py)
from Classes import HelloWorldClass
HelloWorld()
and for some reason, I am getting this error...
Traceback (most recent call last):
File "D:/Coding file/Owl Hoot/Classes/ClassTest.py", line 3, in <module>
from Classes import HelloWorldClass
ModuleNotFoundError: No module named 'Classes'
>>>
Both files are in the Classes file. I don't see what I am doing wrong, can anyone help?

If both are in the same directory you can import like that:
from HelloWorldClass import HelloWorld

Related

Why is my program throwing exception 'xyz' is not a package?

I have the following setup in my Spyder ide:
this program is showing the following exception:
runfile('C:/Users/pc/source/repos/python_package_and_module_test/main.py', wdir='C:/Users/pc/source/repos/python_package_and_module_test')
Reloaded modules: jupyter_client.session, zmq.eventloop, zmq.eventloop.ioloop, tornado.platform, tornado.platform.asyncio, tornado.gen, zmq.eventloop.zmqstream, jupyter_client.jsonutil, jupyter_client.adapter, spyder, spyder.pil_patch, PIL, PIL._version, PIL.Image, PIL.ImageMode, PIL.TiffTags, PIL._binary, PIL._util, PIL._imaging, cffi, cffi.api, cffi.lock, cffi.error, cffi.model
Traceback (most recent call last):
File "C:\Users\pc\source\repos\python_package_and_module_test\main.py", line 1, in <module>
import token.factoryclass as a
ModuleNotFoundError: No module named 'token.factoryclass'; 'token' is not a package
how can I resolve this issue?
source code:
my files are as follows:
.\
python_package_and_module_test.py
import token.factoryclass as a
var = a.factoryclass();
var.print();
factoryclass.py
class factoryclass(object):
def print(object):
print("factoryclass")
tokenclass.py
class tokenclass(object):
def print(object):
print("tokenclass")
factory\
factoryclass.py
class factoryclass(object):
def print(object):
print("factory.factoryclass")
tokenclass.py
class tokenclass(object):
def print(object):
print("factory.tokenclass")
token\
factoryclass.py
class factoryclass(object):
def print(object):
print("token.factoryclass")
tokenclass.py
class tokenclass(object):
def print(object):
print("token.tokenclass")
However, the same package and module structure is working in repl.it:

ModuleNotFoundError: No module named in PyCharm

I was able to run the project using the command line, but from last 1 day I am not able to run the command from the command line However using GUI the project is running fine...
C:\Users\tester\PycharmProjects\Selenium\SampleProjects\POMProjectDemo\Tests>py
thon login.py
Traceback (most recent call last):
File "login.py", line 6, in <module>
from SampleProjects.POMProjectDemo.Pages.loginPage import LoginPage
ModuleNotFoundError: No module named 'SampleProjects'
C:\Users\tester\PycharmProjects\Selenium\SampleProjects\POMProjectDemo\Tests>py
thon -m unittest login.py
E
======================================================================
ERROR: login (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: login
Traceback (most recent call last):
File "C:\Users\tester\AppData\Local\Programs\Python\Python37-32\lib\unittest\
loader.py", line 154, in loadTestsFromName
module = __import__(module_name)
File "C:\Users\tester\PycharmProjects\Selenium\SampleProjects\POMProjectDemo\
Tests\login.py", line 6, in <module>
from SampleProjects.POMProjectDemo.Pages.loginPage import LoginPage
ModuleNotFoundError: No module named 'SampleProjects'
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (errors=1)
[![Tests/login.py
import time
from selenium import webdriver
import unittest
from selenium.common.exceptions import NoSuchElementException
from SampleProjects.POMProjectDemo.Pages.loginPage import LoginPage
from SampleProjects.POMProjectDemo.Pages.homePage import HomePage
import HtmlTestRunner
from SampleProjects.POMProjectDemo.Utility.XLUtil import getData
class LoginTest(unittest.TestCase):
#classmethod
def setUpClass(cls):
cls.driver = webdriver.Chrome(executable_path='F:/Selenium/chromedriver.exe')
cls.driver.implicitly_wait(10)
cls.driver.maximize_window()
def test_login_valid(self):
path = 'c:/Users/mahmood/PycharmProjects/Selenium_automaton/Login.xlsx'
# global path
driver = self.driver
row = getData.getRowCount(path,'Sheet1')
for r in range(2,row+1):
driver.get("https://opensource-demo.orangehrmlive.com/")
userN = getData.readData(path,'Sheet1',r,1)
passW = getData.readData(path,'Sheet1',r,2)
Same code was running fine, what changes I have done not able to debug it.

ModuleNotFoundError: No module named X although the module does exist

I have a very strange error with Python.
python main1.py
Traceback (most recent call last):
File "main1.py", line 1, in <module>
from sfr import hello
File "C:\sfr\hello.py", line 1, in <module>
from constants1 import OBJECT_NAME
ModuleNotFoundError: No module named 'constants1'
My main1.py looks like this
from sfr import hello
print('Hello World')
type sfr\hello.py
from constants1 import OBJECT_NAME
type sfr\constants1.py
OBJECT_NAME = 'salesforce_object_name'
I am unable to fathom this why am i getting no module named constants1 ?
The constants1.py is located in sfr folder
Since you are running the main1.py file from the parent directory (outside sfr), the import path should be from the base. That is,
from sfr.constants1 import OBJECT_NAME
You probably need to give imports from the root path of your project. If sfr is a package at root level, your statement should be:
from sfr.constants1 import OBJECT_NAME

Cyclic import in Python3

I have the following directory structure:
my-game/
__init__.py
logic/
__init__.py
game.py
player.py
game.py and player.py have import dependency on each other (cyclic imports).
game.py has the following definition.
from logic.player import RandomPlayer, InteractivePlayer
T = 8
class Game:
def __init__(self, p1, p2)
...
# some other things
if __name__ == '__main__':
p1 = RandomPlayer()
p2 = InteractivePlayer()
g = Game(p1, p2)
...
player.py is as follows:
from logic.game import T
class Player:
def __init__(self):
...
class RandomPlayer(Player):
def __init__(self):
...
class InteractivePlayer(Player):
def __init__(self):
...
I am trying to run the game from logic/ directory but I get the following error.
$ python3 game.py
Traceback (most recent call last):
File "game.py", line 2, in <module>
from logic.player import RandomPlayer, InteractivePlayer
ModuleNotFoundError: No module named 'logic'
I then tried running game.py from the directory higher up (my-game/).
$ python3 logic/game.py
Traceback (most recent call last):
File "logic/game.py", line 2, in <module>
from logic.player import RandomPlayer, InteractivePlayer
ModuleNotFoundError: No module named 'logic'
What am I doing wrong? How can I make these cyclic imports work?
I have also tried using this import in player.py
from .game import T
and using
from .player import RandomPlayer, InteractivePlayer
in game.py.
In this case, I get a different error. For example, when running from my-game/,
$ python3 logic/game.py
Traceback (most recent call last):
File "logic/game.py", line 2, in <module>
from .player import RandomPlayer, InteractivePlayer
ModuleNotFoundError: No module named '__main__.player'; '__main__' is not a package
I get a similar error when running from logic/ directory.
I looked at this post but didn't understand where I was going wrong.
You are made a circulair import in your code try to remove it from your import
Vist this link you can find some other info about circular import :Remove python circular import

How to call the class choosercolor from module tkinter in python function askcolor?

I want to call the askcolor function to call the color palette:
from tkinter import *
colorchooser.askcolor()
But I get error:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
colorchooser.askcolor()
NameError: name 'colorchooser' is not defined
You are using from tkinter import * that imports from tkinter all methods and variables (actually all public objects not starting with _), but not its submodules. (you can read more here)
So when you try to call colorchooser.askcolor() you have not imported it yet because it's a submodule!
You can change your import and it will resolve to issue!
Try this:
from tkinter import colorchooser
colorchooser.askcolor()
Hope this helps you!

Resources