Im trying to start telegram bot in Linux using venv. But bot starts only if venv activated manualy.
Python code:
#!env/bin/python3
# -*- coding: utf-8 -*-
import config
import telebot
bot = telebot.TeleBot(config.token)
@bot.message_handler(content_types=["text"])
def repeat_all_messages(message):
bot.send_message(message.chat.id, message.text)
if __name__ == '__main__':
bot.infinity_polling()
Bot starts with comands:
root@ubuntu-s-1vcpu-1gb-ams3-01:~/jira_bot# source env/bin/activate
(env) root@ubuntu-s-1vcpu-1gb-ams3-01:~/jira_bot# python3 sreda_bot.py
But if i try to start it without activating venv:
root@ubuntu-s-1vcpu-1gb-ams3-01:~/jira_bot# python3 sreda_bot.py
Traceback (most recent call last):
File "sreda_bot.py", line 4, in <module>
import telebot
ModuleNotFoundError: No module named 'telebot'
3
Answers
Finally I inserted full path to the interpreter in the venv in shebang line:
Used
./sreda_bot.py
instead ofpython3 sreda_bot.py
. And it works fine.The purpose of virtual environments in Python is to create a physical separation between projects and their modules. In this case, the telebot module that you installed in the virtual environment, isn’t in scope (available for use) outside of the virtual environment.
Considering Python Shebang Syntax is like the following
You just need to locate your Virtual ENV‘s interpreter location.
Thus assuming your Virtual ENV is located at
~/jira_bot
base from the following.So your shebang should be
#!/root/jira_bot/bin/python3