Service fails to start due to port 8000 / 8080 already in use.
Kill process on port 8000: sudo fuser -k 8000/tcp
Kill process on port 8000: sudo fuser -k 8080/tcp
Detailed Explanation
When a service doesn't start and complains about a port being in use, it indicates another process is already listening on that port. Port conflicts are common when running multiple services on the same machine, especially during development.
The command sudo fuser -k 8000/tcp
is used to kill the process that is using TCP port 8000. Here's what each part does:
sudo
: Run the command with superuser privileges, necessary for affecting other processes.fuser
: A utility to show which processes use the specified files, sockets, or filesystems.-k
: Kill processes accessing the file (in this case, the port).8000/tcp
: Specifies the TCP port 8000 to be checked and acted upon.
After freeing up the port, you can start your service. If using honcho
, which is a Python tool that manages Procfile-based applications, run honcho start
to start the services defined in your Procfile
.
Note: Be cautious when killing processes, as you might terminate something important. Always check what is running on that port if possible.
Encountering ModuleNotFoundError: No module named 'psycopg'
error in a Python application.
Update the PostgreSQL driver in requirements.txt
and modify the DATABASE_URI
prefix in the application configuration.
-
Change
requirements.txt
:psycopg[binary]==3.1.12
-
Modify
DATABASE_URI
prefix inconfig.py
,test_models.py
,test_service.py
, andsecret.yaml
: Frompostgresql://
topostgresql+psycopg://
.
Detailed Explanation
This issue arises due to the absence or incompatibility of the PostgreSQL driver in your Python application. The solution involves updating to a newer PostgreSQL driver that is compatible with both Intel and ARM (Apple Silicon) platforms and doesn't require additional libraries.
Steps to resolve:
-
Update
requirements.txt
: Change the PostgreSQL driver version topsycopg[binary]==3.1.12
in therequirements.txt
file. This version of the driver is more up-to-date and compatible with a broader range of systems.psycopg[binary]==3.1.12
-
Modify
DATABASE_URI
Prefix: The database URI used in the application needs to be updated to work with the new driver. Change the prefix frompostgresql://
topostgresql+psycopg://
in all instances within the application. This includes files likeconfig.py
,test_models.py
,test_service.py
, and any other place whereDATABASE_URI
is defined, includingsecret.yaml
. -
Apply Changes: After making these changes, ensure to install the updated dependencies using
pip install -r requirements.txt
or a similar command appropriate for your environment. -
Test the Application: Run your test suites and start the application to ensure that the database connectivity issues are resolved.
This solution ensures compatibility across different platforms and simplifies the setup process by eliminating the need for additional libraries. The psycopg2-binary package provides a stand-alone package to interact with PostgreSQL.