Python Forum

Full Version: ModuleNotFoundError: No module named 'com'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello. I have a simple class and a simple test.
I've created packages com.my.pack and class My1.py
class My1:
      val1="ABC"
      val2=12345

      def init(self):
            print("init class My1")
And I've created class TestMy1.py
from com.my.pack import My1

class TestMy1:
      my1 = My1()
When I'm trying to run a test the exception occurs
Error:
from com.my.pack import My1 ModuleNotFoundError: No module named 'com':
What is the problem?
I don't know of any package named com.
perhaps you want pyserial? see: https://pythonhosted.org/pyserial/
Or, for async: https://pyserial-asyncio.readthedocs.io/en/latest/
Where is the com module? Is it in the same directory as the main program or somewhere in the PYTHONPATH so your programs can find it?

Setting PYTHONPATH: https://www.techwalla.com/articles/how-t...ython-path
The code is questionable. Looks like you want to create an object from a class. It has nothing to do with modules.
Python is complaining that it cannot find a module named com. When this happens to me it usually means the com.py module is not in the Python search path (use src.path to see this). I don't know what you mean by "I've created packages com.my.pack". Could you describe further please?
Thank you.
Actually, I'm Java developer. So, as usual, I have created the class in a certain package
package com.my.pack1
class MyClass {
}
Now I'm trying to utilize the same approach in Python.
So I've created the package 'com.my.pack1' and class 'MyClass' there.
I've created the test and trying to launch it. The structure of the project

http://prntscr.com/w17ee3
http://prntscr.com/w17h2m

Maybe some additional settings should be made?
When I test what packages are available from folder main it shows : ['TestPackage', 'com', 'package1', 'setup'] but not 'com.my.pack'.
http://prntscr.com/w18js9
(Dec-10-2020, 06:51 PM)Marbelous Wrote: [ -> ]Where is the com module? Is it in the same directory as the main program or somewhere in the PYTHONPATH so your programs can find it?

Setting PYTHONPATH: https://www.techwalla.com/articles/how-t...ython-path

Thank you. But I need a portable code. Actually, it's a Gradle build. So all settings regarding paths should be configured inside of build and should not depend on settings of OS. Are there any solutions?
You can add directories to the search path at runtime.

import sys
sys.path.append("/path/to/needed/location")
import other_module
You can use pathlib to keep your complete diretory structure relative to a project.
Cross platform safe.
see a former post here: https://python-forum.io/Thread-Automate-...#pid131935