![]() |
'no module named' when creating packages - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: 'no module named' when creating packages (/thread-35675.html) |
'no module named' when creating packages - mbastida - Nov-30-2021 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 gatewayAnd get: Quote:Traceback (most recent call last): Now if I change import magtewayto from gateway import mgatewayI get: Quote:Traceback (most recent call last): 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. RE: 'no module named' when creating packages - mbastida - Nov-30-2021 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 SerialHelperto 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 SerialHelperon 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): What am I doing wrong? RE: 'no module named' when creating packages - Gribouillis - Nov-30-2021 Try this perhaps from . import mgateway RE: 'no module named' when creating packages - mbastida - Nov-30-2021 (Nov-30-2021, 09:52 AM)Gribouillis Wrote: Try this perhaps 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 Creating the package and executing as a package: Quote:(test) pi@raspberrypi:~/Desktop/testGw $ python3 -m gatewaySo still it doesn't import the .py files that are inside the same gateway package RE: 'no module named' when creating packages - Gribouillis - Nov-30-2021 try from . import custom_Logging
|