Python Forum
'no module named' when creating packages
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'no module named' when creating packages
#1
Hi, I have a Python Script that I'm attempting to convert into a package to be uploaded to PyPI.

gateway-software/
┣ gateway/
┃ ┣ GPSTrack.py
┃ ┣ OTA.py
┃ ┣ SerialHelper.py
┃ ┣ UPShat.py
┃ ┣ __init__.py
┃ ┣ __main__.py
┃ ┣ custom_Logging.py
┃ ┣ gatewayID.py
┃ ┣ mgateway.py
┃ ┗ service.py
┣ LICENSE
┣ README.md
┣ pyproject.toml
┣ requirements.txt
┗ setup.py

Some context of the classes.
__main__.py:
import mgateway

if __name__ == "__main__":
    mgateway.main()
mgateway.py has a main() method that starts the whole script.

on the gateway-software folder I have a venv. If I run __main__ from there I have no trouble. Everything works.

Now to the setup.py:
import pathlib
from setuptools import find_packages, setup

# The directory containing this file
HERE = pathlib.Path(__file__).parent

# The text of the README file
README = (HERE / "README.md").read_text()

#get requierements from requirements.txt file
requirements = []
with open('requirements.txt', 'r') as fh:
    for line in fh:
        requirements.append(line.strip())

# This call to setup() does all the work
setup(
    name="industrial-gateway",
    version="0.0.21",
    description="Read the latest Real Python tutorials",
    long_description=README,
    long_description_content_type="text/markdown",
    url="https://[email protected]/citisend/gateway-software.git",
    author="Martí Bastida",
    author_email="[email protected]",
    license="None",
    include_package_data=True,
    classifiers=[
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.7",
    ],
    packages=find_packages(exclude=("tests",)),
    install_requires=requirements,
    entry_points={
        "console_scripts": [
            "gatewayRun=gateway.mgateway:main",
        ]
    },
)
You can see that in theory we have two entrypoints to the scripts. The first is the module name: gateway, the second is the entry_point gatewayRun.

I build and upload the project to a private PyPI repository and pip3 install it into another venv

I run
python3 -m gateway
And get:
Quote:Traceback (most recent call last):
File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/pi/Desktop/testGw/test/lib/python3.7/site-packages/gateway/__main__.py", line 1, in <module>
import mgateway
ModuleNotFoundError: No module named 'mgateway'

Now if I change
import magteway
to
from gateway import mgateway
I get:
Quote:Traceback (most recent call last):
File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/pi/Desktop/testGw/test/lib/python3.7/site-packages/gateway/__main__.py", line 1, in <module>
from gateway import mgateway
File "/home/pi/Desktop/testGw/test/lib/python3.7/site-packages/gateway/mgateway.py", line 11, in <module>
import custom_Logging
ModuleNotFoundError: No module named 'custom_Logging'

Which means that this was a solution, because now the error (which is of the same type as before) is further in the code. It appears as if I should have to change all 'import module' by 'from gateway import module'. How so?
Am I doing something wrong or this is to be expected?

From my understading if it runs as is as a script it should also run as a module.
Reply
#2
So a follow-up on the same problem:
Switching from this:
import custom_Logging
import OTA
import uuid
from GPSTrack import GPSTracker
from service import Service
from UPShat import UPS2_IO
from SerialHelper import SerialHelper
to this:
import gateway.custom_Logging as custom_Logging
import gateway.OTA as OTA
from gateway.GPSTrack import GPSTracker
from gateway.service import Service
from gateway.UPShat import UPS2_IO
from gateway.SerialHelper import SerialHelper
on mgateway.py makes the package work with
Quote:python3 -m gateway

However, the project without the package (so executing the script as a script, not as a module) doesn't work:
Quote:Traceback (most recent call last):
File "/home/pi/Desktop/repo/gateway-software/gateway/__main__.py", line 1, in <module>
import gateway.mgateway as mgateway
ModuleNotFoundError: No module named 'gateway'

What am I doing wrong?
Reply
#3
Try this perhaps
from . import mgateway
Reply
#4
(Nov-30-2021, 09:52 AM)Gribouillis Wrote: Try this perhaps
from . import mgateway

Executing as a script:
Quote:(productionVenv) pi@raspberrypi:~/Desktop/repo/gateway-software $ /home/pi/Desktop/repo/gateway-software/productionVenv/bin/python /home/pi/Desktop/repo/gateway-software/gateway/__main__.py
Traceback (most recent call last):
File "/home/pi/Desktop/repo/gateway-software/gateway/__main__.py", line 1, in <module>
from . import mgateway
ImportError: cannot import name 'mgateway' from '__main__' (/home/pi/Desktop/repo/gateway-software/gateway/__main__.py)


Creating the package and executing as a package:
Quote:(test) pi@raspberrypi:~/Desktop/testGw $ python3 -m gateway
Traceback (most recent call last):
File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/pi/Desktop/testGw/test/lib/python3.7/site-packages/gateway/__main__.py", line 1, in <module>
from . import mgateway
File "/home/pi/Desktop/testGw/test/lib/python3.7/site-packages/gateway/mgateway.py", line 11, in <module>
import custom_Logging
ModuleNotFoundError: No module named 'custom_Logging'
So still it doesn't import the .py files that are inside the same gateway package
Reply
#5
try from . import custom_Logging
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  no module named 'docx' when importing docx MaartenRo 1 715 Dec-31-2023, 11:21 AM
Last Post: deanhystad
  Problem with pymodbus - ModuleNotFoundError: No module named 'pymodbus.client.sync' stsxbel 2 22,951 Nov-02-2023, 08:20 AM
Last Post: South_east
  ModuleNotFoundError: No module named 'requests' Serg 18 2,167 Oct-29-2023, 11:33 PM
Last Post: Serg
  Resolving ImportError: No module named gdb (Python in C++) mandaxyz 3 1,313 Oct-04-2023, 02:43 PM
Last Post: mandaxyz
  ModuleNotFoundError: No module named 'PyPDF2' Benitta2525 1 1,392 Aug-07-2023, 05:32 AM
Last Post: DPaul
  ModuleNotFoundError: No module named 'eyed3' Wimpy_Wellington 2 1,229 Jul-10-2023, 03:37 AM
Last Post: Wimpy_Wellington
  How to fix this error: ModuleNotFoundError: No module named 'notears' yaoyao22 2 964 Jul-09-2023, 11:24 AM
Last Post: yaoyao22
  Help with pyinstaller "No module named" korenron 9 8,217 Jun-15-2023, 12:20 PM
Last Post: snippsat
  Problem with Pyinstaller. No module named '_tkinter' tonynapoli2309 0 934 May-15-2023, 02:38 PM
Last Post: tonynapoli2309
  ModuleNotFoundError: No module named 'omsdk.sdkproto' donvito7 4 1,772 Oct-20-2022, 02:56 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020