Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Crontab and python versions
#1
Good day. Please help me figure this out. Ubuntu has 2 versions of python (3.6 and 3.9). Pymodbus (2.5.3 and 3.0) are installed via pip. So, when I run the script via the terminal, everything is ok, but when I run it via crontab (I specify to run it via python3.9), it takes the 2.5.3 library. How can I make it take the 3.0 library?
Reply
#2
You can make your python script executable, and inside your script you use the path to the python path you desire

For example I have this script that is pointing to the python version I want.

#!/usr/local/bin/python3

import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

host = '192.168.2.34'
port = 443

def portscanner(port):
    # print(sock.connect_ex((host, port)))
    if sock.connect_ex((host, port)):
        print(f"For host {host}:{port} is closed")
    else:
        print(f"For host {host}:{port} is open")

portscanner(port)
Then I made the script executable and does it call it from crontab
Reply
#3
You can use pyenv, and have a different version for each project,
See: pyenv Simple Python Version Management
Reply
#4
(Jun-08-2025, 04:59 PM)stsxbel Wrote: I run it via crontab (I specify to run it via python3.9), it takes the 2.5.3 library.
Specify full path in crontab.
which python3.9
Example output: /usr/bin/python3.9

Eg install pymodbus 3.0 to the correct Python version:
/usr/bin/python3.9 -m pip install pymodbus==3.0
Use the full path in crontab:
* * * * * /usr/bin/python3.9 /path/to/your_script.py
If this not works try using a virtual environment.
python3.9 -m venv /home/youruser/myenv
source /home/youruser/myenv/bin/activate
pip install pymodbus==3.0 
Crontab entry using full path to python in venv.
* * * * * /home/youruser/myenv/bin/python /path/to/your_script.py
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Launcher (on Windows) only necessary when 2 or more Python versions installed? pstein 1 3,765 Feb-17-2024, 12:43 PM
Last Post: deanhystad
Question log.exception() without arguments in old Python versions? cthart 5 3,164 Nov-19-2022, 07:09 PM
Last Post: Gribouillis
  Python versions with libexpat fixes raghupcr 5 8,137 Feb-24-2022, 06:26 PM
Last Post: raghupcr
  Installed versions of Python SamHobbs 5 5,719 Sep-02-2021, 02:28 PM
Last Post: jefsummers
  Architecture question for website with python crontab script rockie12us 1 2,874 Aug-09-2021, 10:07 AM
Last Post: Larz60+
  Different versions of Python3 and Python Led_Zeppelin 2 3,926 Jun-16-2021, 05:31 AM
Last Post: snippsat
  Python Versions ThatTamyBot 2 4,177 Mar-11-2021, 11:27 AM
Last Post: ThatTamyBot
  I seem to have two versions of python 3.7.9 installed miner_tom 7 7,560 Nov-22-2020, 06:42 PM
Last Post: miner_tom
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 3,632 May-28-2020, 05:27 PM
Last Post: micseydel
  python-crontab - How to use @reboot micksulley 2 5,009 Mar-03-2020, 08:06 PM
Last Post: micksulley

Forum Jump:

User Panel Messages

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