Error executing Python script file through Talend tSystem - python

I have a .py file that i am trying to execute through Talend using tSystem module. I am getting below error.
Starting job MyJob at 07:06 20/02/2017.
[statistics] connecting to socket on port 3364
[statistics] connected
Traceback (most recent call last):
File "H:\Talend POC\NameParsingUSData.py", line 7, in <module>
from unidecode import unidecode
ImportError: No module named 'unidecode'
[statistics] disconnected
Job MyJob ended at 07:06 20/02/2017. [exit code=0]
This is how the job looks like.
Its strange, because when i double click the .py file, it executes and gives the result successfully. I also tried executing the file through cmd command line in command prompt, outside talend and it runs successfully as well. Not sure what might be causing the error. For reference below is my Python script.
import os
from unidecode import unidecode
import pandas as pd
dat = pd.read_csv("H:\\Talend POC\\1M_dummyData.csv")
df1.myvar = df1.myvar.str.replace(r"[^a-zA-Z., ]",r' ')
df1.myvar = df1.myvar.str.replace('\s+', ' ')
df1.myvar = df1.myvar.str.lstrip(".,' ")
df=df1.as_matrix()
df1.to_csv("H:\\Talend POC\\results_USvalidemail_parse.csv",header=True,index=False, encoding='utf-8')
Note that it successfully imports the first library, error starts from the second library import 'unicode'
Any help?

I don't know the reason why it works, but this is what my hack was. I searched for any .py file that exists in the Talend data integration dump and i found only one in the directory
C:\Users\user\Downloads\TOS_DI-20161216_1026-V6.3.1\plugins\org.apache.ant_1.9.2.v201404171502\bin
I tried installing the library in this directory in the command line.
Firstly i ran the below code in command line to go to the directory.
cd /d C:\Users\user\Downloads\TOS_DI-20161216_1026-V6.3.1\plugins\org.apache.ant_1.9.2.v201404171502\bin
Secondly, i installed the unidecode library using command pip install unidecode
I re-executed the talend job and it executed with success!!
tarting job MyJob at 08:05 20/02/2017.
[statistics] connecting to socket on port 3983
[statistics] connected
[statistics] disconnected
Job MyJob ended at 08:05 20/02/2017. [exit code=0]

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

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.

Cannot run a python script using crontab

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.

Error when running a python script

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.

knitr - error when importing python module

I am having trouble when running the python engine in knitr. I can import some modules but not others. For example I can import numpy but not pandas.
{r, engine='python'}
import pandas
I get the error.
Quitting from lines 50-51 (prepayment.Rmd)
Error in (knit_engines$get(options$engine))(options) :
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named pandas
Calls: <Anonymous> ... process_group.block -> call_block -> block_exec -> in_dir -> <Anonymous>
In addition: Warning message:
running command ''python' -c 'import pandas' 2>&1' had status 1
Execution halted
It must be something to do with system path and where I am pointing.
Normally in IPython i use
import sys
sys.path.append('/path/to/directory/')
To add paths. I don't know what the problem is.
Use:
{python engine.path="C:/anaconda/python.exe"}
import pandas
change C:/anaconda/python.exe to your python path.
This happened to me too, in Atom.
Check to make sure the two files are in the same directory. Put them in the same folder or path.
Then, go into Terminal or Command Line and type
cd Downloads
(or whatever large directory the files are in). Then, type in smaller folder like: cd _____, such as cd animals. There, you can open the one file that you want to have the other file import to by typing python3 -i ____.py (file name). Make sure the file has from ___(master file) import * and you can run the code in terminal.
This should work. The problem is you can't import by running the code. Instead, you have to link the two using terminal or command line.

Categories

Resources