I've got a Python script that I need to run upon start up and problem
is that it throws an error saying "no module named xyz". I'm using external library which I installed using pip3. The script works just fine on its own
but I get aforementioned error when I want to run it right after boot.
What should I do ? I tried to delay importing the library with time.sleep(10) in case third-party libraries need few more seconds to load up after boot, but that didn't have desired effect.
I run the script on Raspberry Pi with Debian-based os called Raspbian. I configured execution upon start up by adding this sudo python3 script.py into /etc/profile file.
I don't see how pip install without sudo could have worked.
What I see: scripts run on startup from cron or /etc/profile are run under root, not under pi user. Thus, they don't have the same $PATH, $PYTHONPATH and other environment variable values that you have in your user's shell.
As pip install managed to run without sudo, I suspect that you installed your module into a user-specific directory, which is not a part of root's Python environment.
Replacing the /etc/profile line with sudo -u pi python3 script.py may help.
Whatever it is, it's the difference that is already in the Python environment. Waiting for 10s "for whatever to come up" will not help it.
1 - Download anaconda: https://www.anaconda.com/download/
2 - Create an environment in conda: https://conda.io/docs/user-guide/tasks/manage-environments.html
3 - Activate that environment
4 - conda install or pip3 install your package
It should just work.
Related
I am running Gitbash for windows and have installed pipenv using pip. Yet when I invoke pipenv nothing happens:
Since there isn't a command not found error, I believe the script is recognized it just isn't running properly.
Pipenv is installed globally:
I also can confirm that the Scripts folder is in the file path:
I don't think it is a problem with Gitbash because I can run other pip packages in the same scripts folder:
I'm also able to run python pipenv.exe but not pipenv.exe when in the Scripts folder:
I've struggled with this myself untill just now.
I've got a few suggestions.
SOLUTION 1:
I was calling Poetry to try and make sense of it as well and then I tried calling both applications from Powershell. Both work as inteded.
SOLUTION 2:
Calling python -m pipenv, as sugested in this other Stack Overflow thread, also works as intended. You could alias the command to pipenv in git bash and call it a day too.
Quoting the docs on the -m flag:
When called with -m module-name, the given module is located on the Python module path and executed as a script.
Quoting appdividend.com:
The -m flag in Python searches the sys.path for the named module and executes its contents as the __main__ module.
It goes through whatever path is in which pipenv and executes the module as a script.
SOLUTION 3:
This is how I ended up fixing my Pipenv blunder.
Reinstall Python and all packages to AppData.
MINGW64 was having trouble seeing Pipenv in C:\Program Files. I also noticed that I had some packages in AppData\Roaming as well, so I figured I'd reinstall Python, unticking the Install for all users option (to trigger AppData installation) to see if I could wrangle all packages together.
After that I tried installing Pipenv and succeeded in calling it as expected.
This is all highly anecdotal. I have no idea why MINGW64 failed to call JUST Pipenv and not Poetry, but this is how I've fixed it on my end.
If I install a virtualenv on my local machine, activate it and try to run python3 then it works fine (with the imported modules). However, after I send it to the live server (using scp and filezilla) it gives the error:
-bash: /<path>/venv4/bin/python3: cannot execute binary file: Exec format error
This also happens with python and python3.8 in the same package.
I have tried reinstalling virtualenv and pipx, recreating the virtualenv and reuploading a few times.
It seems that it can't find the module, as when I activate the virtualenv on the live server and type "which python3" then it shows me the system python3:
/usr/bin/python3
It also does not work if I try to execute the venv's python3 directly, using the full path.
The reason I'm doing this is because the old virtualenv I was using has stopped working because it can't seem to find the installed modules anymore. I'm not sure why.
Any help would be much appreciated.
I believe some pip packages contain more than just python code, and must be compiled. If your host OS is different from your server OS, or you have different libraries installed, the host-compiled code will not be compatible with your server.
Common practice is to create a file with a list of required packages, using something like
pip freeze > requirements.txt
and rebuild the environment on the server, using something like
pip install -r requirements.txt
I have a python script called main.py that imports RPi.GPIO library using import RPi.GPIO as GPIO
When I run the script using python3 main.py I get an error that states RPi.GPIO was not found. If I run main.py using sudo python3 main.py then everything runs fine.
I installed RPi.GPIO using a tar.gz file. I copied it to my /home/pi/work directory and extracted the tarball in the same directory. I then changed to the extracted directory and installed RPi.GPIO using sudo python3 setup.py install.
As I don't normally use linux I don't fully understand the permissions which I am sure is what is causing this issue. I am guessing that since I installed using sudo that the package is only available to the sudo user. The problem is I am starting this program from rc.local file and the main.py script wont run at startup with the RPi.GPIO import statement. If I remove the import statement it starts as expected. Below is the code in the rc.local file that starts the program su -l pi -c '/usr/bin/python3 /home/pi/Work/main.py &' I tried changing su to sudo but that did not work.
Is there a different way I can install RPi.GPIO or change the rc.local script to get this working? Also FYI my pi has no internet connection so I can't use APT-GET to uninstall or install the package.
Also just in case some of you wonder if the package installed properly it has. If I start python with sudo python3 I get >>>. I then type import RPi.GPIO as GPIO I get >>> again. Then I type GPIO.VERSION it displays the correct version I installed. Any help would be appreciated.
UPDATE
I did not create the code for the rc.local file and looked a little closer at it. The statement su -l pi -c '/usr/bin/python3 /home/pi/Work/main.py &' I found out changes the user from root to pi and executes the script under the pi user. So I tried changing the statement to su -l sudo -c '/usr/bin/python3 /home/pi/Work/main.py &' thinking that since I can run the main.py by using sudo python3 main.py that changing user from pi to sudo in the rc.local file would execute the file as sudo. It still does not work. I then tried removing the su command from the rc.local command and ran like this /usr/bin/python3 /home/pi/Work/main.py & but this also didn't work. Does anyone have any suggestions on how I can get this to work?
Try to write bash script with sleep before running python script and put it into rc.local
#!/bin/sh
sleep 5
python python_script.py &
Don't forget to make script executable: chmod 755 yourscript.sh
For developing in Python, on Linux or any OS, one would almost always use a virtualenv, one for each python project I want to develop.
A virtualenv is easy to set up, and once activated, you can execute your pip install commands without using sudo. Try setting up a virtualenv and installing GPIO via pip.
Even if you don't set up a virtualenv to help with package management for your Python project, you can still use pip to install GPIO, but you'll face the permission issues you're dealing with now.
But, you're in luck! GPIO is already installed on Raspbian. Open up the Terminal and type python. Once you're in the Python interpreter, type import RPi.GPIO. If you get an error, there is a genuine issue with your installation, but it can be easily overcome by using a virtualenv. It may help to know which is your default python, with python --version.
Pip is conceptually similar to the apt package manager you've probably used with your Raspberry Pi to install other pieces of software. It's a package manager for Python, basically a registry of libraries you can install instantly on the command line.
The command line is your friend on an RPi, especially when it comes to developing original software.
Good luck!
I tried to set up a virtual environment for my project by executing virtualenv myenv. The folder seemed to be generated, but the command hung, and I couldn't execute another command. I had to close the console and restart cmd. The folder was generated, as I said, but I couldn't activate the virtual environment by venv\Scripts\activate.
I met the same behaviour while trying to execute pip freeze > requirements.txt. The file was generated, but it was empty, although I used a lot of packages in my project. When I executed just pip freeze, the list of packages was printed, but the command hung again, and I had to close the console again.
I tried both procedures many times, but with no success. I tried that in Windows cmd and Anaconda Prompt (Anaconda version: Anaconda3 2.4.1; Python: 3.5.1).
EDIT: when I tried to do this for the very first time some days ago, I succeeded in activating the virtual environment, but only for one time.
A simple solution to this (which does work) would be to use Powershell as an Administrator, instead of cmd.
Conversely, use cmd as an administrator, though I would recommend using the much-more powerful Powershell for any and all purposes!
Why this works:
A lot of commands need super-user rights (think root/ sudo in linux) in order to be properly executed.
Since there is no such thing as as sudo in Windows yet, you can implement it via admin privileges.
Cheers!
I've been using python in both my office pc and my personal laptop and on both machines typing in powershell:
pip install somepackage
works normally.
Strangely enough, when I attempt to do it on my personal desktop, powershell remains on halt infinitely.
I found a solution for this by doing:
python -m pip install somepackage
The above is fine, and I can live with typing an extra 8 characters whenever I want to install a new package but I'm curious to understand what is happening.
Thanks :)
PS: I have added C:/Python27/Scripts to Path in the system variables already and it didn't solve the issue.
pip.exe is located in C:\Python27\Scripts. You need to add that folder to your PATH environment variable. If you added it to the system environment you need to start a new PowerShell console to make the change become effective.
If you want the path to become available in the current console you need to add it to the PATH variable in the current console as well (the console gets a copy of the environment when it starts, it doesn't pick up changes to the system or user environment at runtime):
$env:Path += ';C:\Python27\Scripts'
pip install somepackage
However, C:\Python27\Scripts not being included in the PATH should only result in an error message that PowerShell doesn't recognize the command. It shouldn't freeze the console. Perhaps there's some other cmdlet/command/function/alias named pip that gets executed instead. Try running Get-Command pip to verify that.