I am working on creating a bash file on my raspberry pi in order to be able to launch a webapp project that I have made when the raspberry pi starts up. I have been able to figure out most of it, except for I have issues with the virtual environment letting me access my packages that were installed in the virtual environment. When I run
workon tm
python main.py
in the terminal I am able to get my project working fine. But when I run my bash file
#!/bin/bash
# startup.sh
#Open Chromium on the Raspberry Pi on fullscreen on bootup. Then open the virtual environemnt, and then run our python script
#su - pi -c "/usr/bin/chromium-browser --start-fullscreen 127.0.0.1:5000"
cd /
cd /home/pi/MySQLAppTrueTrue
source /usr/local/bin/virtualenvwrapper.sh
source ~/.bashrc
#workon /home/pi/.virtualenvs/tm
workon tm
sudo python /home/pi/MySQLAppTrueTrue/main.py
I get the error
pi#raspberrypi:~/MySQLAppTrueTrue $ ./startup.sh
Traceback (most recent call last):
File "/home/pi/MySQLAppTrueTrue/main.py", line 1, in <module>
from webapp import app #import the web app
File "/home/pi/MySQLAppTrueTrue/webapp/__init__.py", line 2, in <module>
from flask_mysqldb import MySQL #import mySQL
ImportError: No module named flask_mysqldb
Does this mean that my workon tm line isnt actually causing the device to be working on the virtual environment. If so how do I get that to work? Or do I just need to install my packages globally instead of in the virtual environment?
Instead of all that virtualenvwrapper workon magic, you should probably just use that virtualenv's Python interpreter:
#!/bin/bash
cd /home/pi/MySQLAppTrueTrue
sudo /home/pi/.virtualenvs/tm/bin/python /home/pi/MySQLAppTrueTrue/main.py
Related
I am using Apple M1. Here is the bash script I wrote.
#!/bin/sh
# Open Activity Tracker website
python -m webbrowser http://127.0.0.1:8000/
# Activate virtual environment
. dj-env/bin/activate
# Navigate to Activity Tracker project folder
cd dj_activity_tracker
# Run Django local development server
python manage.py runserver
When you run manually are you doing that in the same virtualenv? Yes.
Are you doing the installs into the virtualenv? Yes.
What version of psycopg? Within my virtual environment, I run "pip show psycopg2-binary". It outputs "Version: 2.9.5".
When I run these commands on my terminal manually without the script, it works. However, when I use this script, I get the error:
File "/Users/doge/Desktop/dj-env/lib/python3.11/site-packages/django/db/backends/postgresql/base.py", line 28, in \<module\>
raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: dlopen(/Users/doge/Desktop/dj-env/lib/python3.11/site-packages/psycopg2/\_psycopg.cpython-311-darwin.so, 0x0002): symbol not found in flat namespace (\_PQbackendPID)
Things I have tried:
Uninstall/reinstall psycopg2
brew install postgresql
I am stumped on how this is working manually but not when I run the script. What could be the issue? I appreciate your support.
Im trying to create a cron job that runs every 15 minutes
*/15 * * * * cd Desktop && /usr/bin/python myFile.py >> myLog 2>&1
When this tries to run, I get this error in myLog
Traceback (most recent call last):
File "myFile.py", line 1, in <module>
import psycopg2
ImportError: No module named psycopg2
Even though when I run it manually in visual studio code with command python myFile.py it runs with no errors and has no issues with psycopg2...any idea whats happening?
Your system might be using Python2 by default at /usr/bin/python. Check if that is the issue and change it to the path either your Python3 executable path or the path of your virtual environment's executable and run.
Also, a better approach would be to have a shell script be executed via crontab and that script internally either activates your virtual environment or runs the program directly after cd-ing into the directory
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'
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.
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
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.
When installing gcloud for mac I get this error when I run the install.sh command according to docs here:
Traceback (most recent call last):
File "/path_to_unzipped_file/google-cloud-sdk/bin/bootstrapping/install.py", line 8, in <module>
from __future__ import absolute_import
I poked through and echoed out some stuff in the install shell script. It is setting the environment variables correctly (pointing to my default python installation, pointing to the correct location of the gcloud SDK).
If I just enter the python interpreter (using the same default python that the install script points to when running install.py) I can import the module just fine:
>>> from __future__ import absolute_import
>>>
Only other information worth noting is my default python setup is a virtual environment that I create from python 2.7.15 installed through brew. The virtual environment python bin is first in my PATH so python and python2 and python2.7 all invoke the correct binary. I've had no other issues installing packages on this setup so far.
If I echo the final line of the install.sh script that calls the install.py script it shows /path_to_virtualenv/bin/python -S /path_to_unzipped_file/google-cloud-sdk/bin/bootstrapping/install.py which is the correct python. Or am I missing something?
The script uses the -S command-line switch, which disables loading the site module on start-up.
However, it is a custom dedicated site module installed in a virtualenv that makes a virtualenv work. As such, the -S switch and virtualenvs are incompatible, with -S set fundamental imports such as from __future__ break down entirely.
You can either remove the -S switch from the install.bat command or use a wrapper script to strip it from the command line as you call your real virtualenv Python.
I had the error below when trying to run gcloud commands.
File "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/lib/gcloud.py", line 20, in <module>
from __future__ import absolute_import
ImportError: No module named __future__
If you have your virtualenv sourced automatically you can specify the environment variable CLOUDSDK_PYTHON i.e. set -x CLOUDSDK_PYTHON /usr/bin/python to not use the virtualenv python.
In google-cloud-sdk/install.sh go to last line, remove variable $CLOUDSDK_PYTHON_ARGS as below.
"$CLOUDSDK_PYTHON" $CLOUDSDK_PYTHON_ARGS "${CLOUDSDK_ROOT_DIR}/bin/bootstrapping/install.py" "$#"
"$CLOUDSDK_PYTHON" "${CLOUDSDK_ROOT_DIR}/bin/bootstrapping/install.py" "$#"
I followed this guide: guide to create a startupfile which excecutes a python file on startup.
in step 2 it says I have to test the startupfile I just created and suddently my script says:
Traceback (most recent call last):
File "Display.py", line 1, in <module>
import pyowm
ImportError: No module named pyowm
the python file works perfect if I run it directly.
what I allready tried: run pip again to see if the lib was okay
check the /usr/local/lib/python3.4/dist-packages folder to see if it was there and it is.
I think this is a python issue and not a RaspberryPi issue thats why I uploaded it here.
runned by:
sh launcher.sh
inside is:
#!/bin/sh
# launcher.sh
# navigate to home directory, then to this directory, then execute python script, then back home
cd /
cd /home/pi/arduino/Python/Main/Master
sudo python Display.py
cd /
Simple fix: define the version of python which will be used. It used python 2.7. Yet the lib was for 3.4.