Error when running a python script - python

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.

Related

Imported module cannot be found when Python script is run via a bat file

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

How to run a python program from python 3.8.0 IDLE shell

I want to launch the program Tip_and_Tax_Calculator.py from a folder named Python27 in a python 3.8.0 IDLE shell but I want to know which code will work. I have tried the exec function and execFile but they do not work, I don't know if I am using them in the wrong way too.
import Tip_and_Tax_Calculator
exec('C:\Python27\Tip_and_Tax_Calculator.py')
The error I get is:
Traceback (most recent call last): File "C:/Users/Jayan
Subramanian/AppData/Local/Programs/Python/Python38-32/Project
Launcher.py", line 1, in import Tip_and_Tax_Calculator
ModuleNotFoundError: No module named 'Tip_and_Tax_Calculator
you could use the exec function combined with open and read in the following way for python 3.x:
Notice I change from \ to /:
exec(open('C:/Python27/Tip_and_Tax_Calculator.py').read())
The method you tried and specified is relevant for python 2.x.
Another issue, you do not need to import the Tip_and_Tax_Calculator
EDIT
after the user added the error message the problem that the script didn't run is the fact the module Tip_and_Tax_Calculator is not in the current shell directory, so one need to specify its full/relative path

Python script within a Pipenv as a windows service with NSSM

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.

Why does eclipse have such a issue importing standard python libraries

I just moved from sublime to eclipse and ran a program which contained the time library 'Arrow'.
It first said the no module could be found, so I then I added the source folder to the PyDev python path, now it's giving me this error:
Traceback (most recent call last):
File "C:\Users\David\workspace\Loan_rates\master.py", line 8, in <module>
import arrow
File "C:\Python27\Lib\site-packages\arrow\arrow.py", line 16, in <module>
from arrow import util, locales, parser, formatter
ImportError: cannot import name util
I think the problem is that you added:
C:\Python27\Lib\site-packages\arrow to your Interpreter PYTHONPATH -- which makes it resolve C:\Python27\Lib\site-packages\arrow\arrow.py as the arrow module.
The solution is removing C:\Python27\Lib\site-packages\arrow from the PYTHONPATH -- C:\Python27\Lib\site-packages should be enough in this case.
Try comparing the command line that works (when launched successfully from your terminal) to what PyDev is constructing.
To see the command line PyDev actually uses, open the launch configuration (Run menu -> Run Configurations... -> Python run -> Select your launch on the left) and in the Interpreter tab press See resulting command-line for the given parameters.
This is a screenshot of what I am referring to:

importError using a virtualenv from cmd on Windows

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.

Categories

Resources