Using How do you run a Python script as a service in Windows? I can get a python script to run as a service. Tested it with the following code I made:
import os
from time import sleep
from random import *
# import flask <-- This line breaks it only when run from NSSM
count = 0
while True:
num = randint(1, 10000)
count+=1
os.mkdir("C:\\temp\\" + str(count) + '_' + str(num))
sleep(2)
I tested the executable and arguments to put into NSSM by first running the following:
cd C:\pipenvfolder\foo
C:\Users\Username\AppData\Local\Programs\Python\Python36\Scripts\pipenv.exe
run python main.py
And it starts the script successfully, even if it has imports to packages installed in the pipenv (e.g. flask). I then created a NSSM service with:
nssm.exe install ServiceName "C:\Users\Username\AppData\Local\Programs\Python\Python36\Scripts\pipenv.exe"
"run python main.py"
nssm set ServiceName AppDirectory
"C:\pipenvfolder\foo"
And every 2 seconds it creates a directory in c:\temp. All is good. However now I wish to import one of the installed Pipenv packages, i.e. the flask package installed within the pipenv. So I added "import flask" to the test script above.
I then set up NSSM to have an error log and checked why it was failing to start, and it is failing to import the flask module:
Traceback (most recent call last):
File "main.py", line 7, in <module>
import flask
ModuleNotFoundError: No module named 'flask'
The nssm service must be starting in the correct app directory or else it would not find main.py. Calling it from the correct directory is what specifies the pipenv. Hence I cannot figure out why the pipenv is not being used to run the script in the same way as when run via the command line.
Create a batch file which calls your virtual environment.
Get the virtualenv path:
pipenv --venv
service.bat
call path/to/.virtualenv/Scripts/activate.bat
call python main.py
Install the service with nssm which calls this batch file.
I doubt this is going to get any answers, but if someone else has the same issue.
I got around the issueby making an exe using pyinstaller. It is fairly quick and easy to do. Then I passed the .exe into NSSM as the executable to be run.
Related
I know there has been similar problems, but unfortunately most of them are related to errors with pyperclip itself instead of the batch file, which i suspect is where the problem stems from.
Below is an MRE of my Python script:
#! python3 -> Do I have to use my version(3.8)?
# pw.py - An insecure password locker program.
import sys, pyperclip
#do something with the module
And my batch file pw.bat:
#py.exe C:\Users\KEVIN\PycharmProjects\atbs_exercise\pw.py %*
#pause
I am running python 3.8 on windows 10. I imported the pyperclip module in my python script pw.py and ran the file via pw.bat, and this in turn gives me this error:
Traceback (most recent call last):
File "C:\Users\KEVIN\PycharmProjects\atbs_exercise\pw.py", line 7, in <module>
import sys, pyperclip
ModuleNotFoundError: No module named 'pyperclip'
Press any key to continue . . .
Which shouldn't happen as I have installed pyperclip on the project using pip, and the script itself runs just fine in pycharm. What am I missing?
EDIT: I forgot to mention that I am using pycharm. So the thing is that pycharm had also installed python.exe in the project folder. And as the module pyperclip is only installed to that folder, the python.exe used in the bat must point to the one in the project folder.
i don't know why are you using py.exe. when running commands from a batch file or cmd .you should use python.exe.obviously you would need to add python to add for doing so.instead of adding py.exe to path,add python in system variable Path which is somewhere present in C:\Users\[username]\AppData\Local\Programs\Python\(your path might be diffrernt).you can add python in Path by following this post
after adding python to path just use the following batch-file:
#echo off
python path-to-your-py-file\filename.py
I am building a Flask app to create a peer to peer market place. I am having trouble getting the code running on my new computer. The code can be found here: https://github.com/ripemelon/flaskbyge.
flaskbyge$ python run.py
/home/byge/.local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py:834: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
Traceback (most recent call last):
File "run.py", line 1, in <module>
from flaskbyge import app
File "/home/byge/Desktop/flaskbyge/flaskbyge/__init__.py", line 15, in <module>
from flaskbyge import routes
File "/home/byge/Desktop/flaskbyge/flaskbyge/routes.py", line 33
flash(f'Account created for {form.username.data}! You can now log in','success' )***
It gives me this error when I try to run the Flask module using the SQLAlchemy database.
You are using Python2.7 as you can see in the first line of your error message. It seems that this app is developed for at least Python3.6, because formatted strings are used as you can see in the last line (flash(f'Account ...)).
This works for me:
$ cd $ROOT_FOLDER_OF_YOUR_APP
$ python3.8 -m venv flaskbyge
$ source flaskbyge/bin/activate
$ pip install -r requirements.txt
$ python run.py
For me python3.8 is clearly running the Python3.8 interpreter. The command can be different for you, maybe python3 or similar. Maybe Python3.6 is enough. Note, that after activating the virtual environment, the python and pip command refers to the right Python version (here Python3.8).
All after all, it seems that you only have to ensure that you don't start the app via Python2.7.
I am struggling to run the a python script as a cron job.
I am logged in as root
the permission for the python script is
-rwxr-xr-x 1 root root 2374 Mar 1 22:49 k_collab_spark.2.py
I am starting the script with
#!/usr/bin/env python
I tested the pythong script
if i do "./k_collab_spark.2.py` this work fine.
on the crontab i have set the job as
15 12 * * * /opt/lampp/htdocs/testme/SPARK/k_collab_spark.2.py >> /var/log/kspark.log
I do not see any message on the log file
Once i adde 2>&1 it gives an error Traceback (most recent call last):
File "/opt/lampp/htdocs/kabeer/SPARK/k_collab_spark.2.py", line 2, in
import requests
ImportError: No module named requests but if i execute the service manually it is successful . WHen i run it manually it works fine
Tried defining the path but still the same issue
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
import requests
ImportError: No module named requests
Any idea what i am missing.. Appreciate any help around this.
Try to run script with another first line:
#!/usr/bin/python
If it's executes successfully the problem in python interpreter, because when you have several versions of Python installed, /usr/bin/env will ensure the interpreter used - is the first one on your environment's $PATH, which i guess has no requests lib.
Can you add python explicitly before the script name?
At the end of the crontab line, add 2>&1, which redirects error messages to the log file as well. See this link for a detailed description In the shell, what does " 2>&1 " mean?
There is also a possibility that your current user and root runs different versions of python.
I used a shell script to call the python script. THe anaconda on the box was causing the trouble
export PATH=/opt/anaconda3/bin:$PATH
/opt/anaconda3/bin/python /opt/lampp/htdocs/scriptme.py >/opt/lampp/htdocs/scriptme.log 2>&1
Add the following lines of code to your script and edit the crontab :
from distutils.sysconfig import get_python_lib
print(get_python_lib())
Now check the log in crontab, you will get some path
e.g. "/usr/lib/python2.7/dist-packages"
cd(change directory) to the above path and ls(list directory) to check if package exists ; if not :
sudo pip3 install requests -t . # dot indicates current directory
or else if you have a requirements.txt file then you could try:
sudo pip3 install -r requirements.txt -t "/usr/lib/python2.7/dist-packages"
#try this from the directory where "requirements.txt" file exists
Now run your scripts.
I am trying to run a script using an interface created with tkinter. I have a button that executes a script which code is:
subprocess.call("python3 " + PATH_TO_SCRIPTS + "main.py 1 &", shell=True)
However, when this button is pressed I am getting the following error.
Traceback (most recent call last):
File "/home/m//PycharmProjects/ROSAutonomousFlight/catkin_ws/src/ardrone_numeric_method_controller/scripts/main.py", line 17, in <module>
from controller import *
File "/home/m/PycharmProjects/ROSAutonomousFlight/catkin_ws/src/ardrone_numeric_method_controller/scripts/controller.py", line 5, in <module>
import rospy
It says that the module rospy does not exist, but when I run
import rospy
using python or python3 it is imported successfully. What can I do to solve this issue? I am using Ubuntu.
The comments to your question are mostly about Python, but I guess it is more of a ROS issue.
You don't have to set-up your PYTHONPATH manually to find rospy but you have to source the setup.bash of your catkin workspace (otherwise none of the ROS tools is found).
Usually this is done by adding something like
source ~/catkin_ws/devel/setup.bash
to .bashrc. This works fine for everything that is run in a terminal.
I don't know how you start your script but as it provides a graphical interface you probably just run it by double-clicking it in the file browser? If you indeed do so, the script is not run in a terminal and therefore can't find the ROS modules. Run the script from a terminal (in which the setup.bash has been sourced) and it should work.
trying to run a flask dev server on localhost:5000 using a virtualenv on Windows 7
In my command line in the project directory, i activated the virtualenv with the command "env\scripts\activate". It seemed to work, as my next line was preceded with an (env) tag. When I attempted to run the app file (bank_app), however, I got an import error.
Here's the console log
C:\Users\TJ\Documents\Python Projects\TestingPython> env\scripts\activate
(env) C:\Users\TJ\Documents\Python Projects\TestingPython> bank_app
Traceback (most recent call last):
File "C:\Users\TJ\Documents\Python Projects\TestingPython\bank_app.py", line 1, in <module>
from flask import Flask
ImportError: No module named flask
and here's a gist of the bank_app file (just in case it's relevant)
I'm used to running the code directly from PyCharm, which handles the virtualenv for me. it works fine running directly from PyCharm
This is probably related to how Windows maps extensions to executables.
You started the script with bank_app, which is really not the name of your script (your script has a .py extension I assume?). Windows must be doing a search in your directory, and then starting the script with the interpreter that is registered for the .py extension of the script, which is the globally installed interpreter, not the interpreter that is currently in the PATH.
I'm pretty sure if you run the command as python bank_app.py everything will work just fine.