2

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'
Vadim
  • 71
  • 1
  • 4
  • 1
    Does this answer your question? [Activate a virtualenv with a Python script](https://stackoverflow.com/questions/6943208/activate-a-virtualenv-with-a-python-script) – mad_ Jun 03 '20 at 14:41
  • 1
    Use the full path to the interpreter in the venv to run it. – Klaus D. Jun 03 '20 at 14:42
  • @KlausD. I tryed (#!/root/jira_bot/env/bin/python3), but it doesnt help. – Vadim Jun 03 '20 at 14:50
  • Don't run your script from `python3 ...`, if you haven't activated the `venv` `python3` will still be pointing to default. Just run the script itself like the answer says, `./sreda_bot.py`. As long as your `env` path in the shebang line is right, it should work. Failing that, try using the `python3` (or `py`, can't remember which for unix) that's *in* your `venv` to call the script, i.e. `/root/jira_bot/env/bin/python3 sreda_bot.py`. – r.ook Jun 03 '20 at 15:36
  • @KlausD.Finaly it helped. I added full path to shebang line. And used "./sreda_bot.py" to start it. Thanks! – Vadim Jun 03 '20 at 15:41
  • @r.ook Thank you. I realized my mistake at the time when you wrote your answer. – Vadim Jun 03 '20 at 15:55

3 Answers3

5

Finally I inserted full path to the interpreter in the venv in shebang line:

#!/root/jira_bot/env/bin/python3

Used ./sreda_bot.py instead of python3 sreda_bot.py. And it works fine.

Vadim
  • 71
  • 1
  • 4
2

Considering Python Shebang Syntax is like the following

#!interpreter [optional-arg]

You just need to locate your Virtual ENV's interpreter location.

#!<venv path>/bin/python[3.x]

Thus assuming your Virtual ENV is located at ~/jira_bot base from the following.

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

So your shebang should be #!/root/jira_bot/bin/python3

0

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.

  • Thank you, i know. But as i know script can be started without activating venv. – Vadim Jun 03 '20 at 14:52
  • You can install the telebot module (`pip install telebot`) outside of your virtual environment, then that module should be available, and then your bot would run successfully. – G Scott Walters Jun 03 '20 at 15:17
  • Yes, i know. I need to start my script inside venv without manualy activating venv if its possible. – Vadim Jun 03 '20 at 15:22
  • I don't think that's possible unless you wrap the execution of the python script in a shell script. `#!/bin/bash cd $PATH-to-venv source env/bin/activate python3 sreda_bot.py` – G Scott Walters Jun 03 '20 at 18:17