Nov-30-2021, 08:26 AM
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:
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:
I build and upload the project to a private PyPI repository and pip3 install it into another venv
I run
Now if I change
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.
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):
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 magtewayto
from gateway import mgatewayI 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.