The problem:
I have a Jenkins build step that executes a shell script. This script in turn calls a python script which is performing some cryptographic functions. However when the build executes, I am getting the following error.
Traceback (most recent call last):
File "./xyz.py", line 4, in <module>
import rsa
ImportError: No module named 'rsa'
The Jenkins' node has two versions of python - 2.7 (default) and 3.4 and rsa is installed for both of them. I even ran the script (using version 3.4) on the slave itself and it worked fine.
What I have done so far:
I am using EnvInject plugin to point PYTHONPATH to correct location. Without it, I found that PYTHONPATH was undefined.
With Python 2.7
Using default version, my script starts with: #!/usr/bin/env python
Jenkins output:
[EnvInject] - Executing scripts and injecting environment variables after the SCM step.
[EnvInject] - Injecting as environment variables the properties content
PYTHONPATH=/usr/local/lib/python2.7
[EnvInject] - Variables injected successfully.
[demo] $ /bin/sh -xe /tmp/hudson9217742060700174209.sh
+ export PYTHONPATH=/jenkins/workspace/demo:/usr/local/lib/python2.7
+ echo /jenkins/workspace/demo:/usr/local/lib/python2.7
/jenkins/workspace/demo:/usr/local/lib/python2.7
+ ./abc.sh
/usr/bin/env: python: No such file or directory
With Python 3.4
The Shebang in this case is #!/usr/bin/env python3
Jenkins output:
[EnvInject] - Executing scripts and injecting environment variables after the SCM step.
[EnvInject] - Injecting as environment variables the properties content
PYTHONPATH=/usr/local/lib/python3.4/
[EnvInject] - Variables injected successfully.
[demo] $ /bin/sh -xe /tmp/hudson4592372533933414288.sh
+ export PYTHONPATH=/jenkins/workspace/demo:/usr/local/lib/python3.4/
+ echo /jenkins/workspace/demo:/usr/local/lib/python3.4/
/jenkins/workspace/demo:/usr/local/lib/python3.4/
+ ./abc.sh
Traceback (most recent call last):
File "./xyz.py", line 4, in <module>
import rsa
ImportError: No module named 'rsa'
I even tried doing sys.path.append(os.environ['/usr/local/lib/python3.4/dist-packages/rsa']) in the script itself, but the problem persists.
Can anyone help me with this issue? Thanks.
P.S.- My knowledge of Python is very limited.
As far as I can remember it, I had similar problems with my Python-script and I finally solved it by using
python xyz.py
instead of
./xyz.py
Unfortunately I can not explain why it works in this way, but not the other, but in my case it solved the problem.
Related
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.
I am trying to run a code I downloaded from https://github.com/szieleniewski/NIFSIM to analyze data from JWSTelescope. While running the main file, jwsim.py, I get a module error on one of the definitions.
I am currently using Python 3.7, Ubuntu 16.04, and I work under an environment (called webbpsf-env). These details can not be changed, since WebbPSF (a program I am using with the code) requires such versions to work. The main jsim.py file is stored in the Downloads folder.
I get the following error:
(webbpsf-env) aleejandrof#aleejandrof-Satellite-C55t-B:~/Downloads/NIFSIM-master$ python jsim.py
*File* "jsim.py", line 18, in <module>
*from* src.JSIM_main *import* main
File "/home/aleejandrof/Downloads/NIFSIM-master/src/JSIM_main.py", line 22, in <module>
*from* JSIM_Specres *import* spectral_res
ModuleNotFoundError: No module named 'JSIM_Specres'
The mentioned line is this one:
from JSIM_Specres import spectral_res
I've been told, that if I work ouside the environment, the module could be found. For me this is not an option since Webbpsf requires working inside an environment. I have tried downloading the master file again inside the environment (through pip), but it could not be done. Is there a way to "insert" a file into an environment? what should I do?
The author likely anticipated you had a certain env var set. Try this:
$ cd ../NIFSIM-master && env PYTHONPATH=.:src python jsim.py
This is a very basic question on how to code in python and run your script from a very beginner.
I'm writing a script using Xcode9.4.1 which is supposed to be for python3.6. I then have an sh script run.sh, in the same folder of the script (say "my_folder") which simply looks like
python my_script.py
The python script looks like
from tick.base import TimeFunction
import numpy as np
import matplotlib.pyplot as plt
v = np.arange(0., 10., 1.)
f_v = v + 1
u = TimeFunction((v, f_v))
plt.plot(v, u.value(v))
print('donne!\n')
But as I try to run my_script.sh from the terminal I get a "ImportError: No module named tick.base" error.
But the tick folder is actually present in "my_computer/anaconda3/lib/python3.6/site-packages" and up to last week I was using Spyder from anaconda navigator and everything was correctly working, so no "import error" occurred.
The question is quite trivial, in some sense it simply is "what's the typical procedure to code and run python script and how modules are supposed to be imported-downloaded when running on a given machine?"
I need it since my script is to be run on another machine through ssh and using my laptop to make some attempts. Up to last year I used to work in C and only need to move some folders with code and .h files.
Thank for help!
EDIT 1:
From the Spyder 3.2.7 setting, where the script was giving non problem, I printed the
import sys
print(sys.path)
The -manually- copied the content to the sys.path variable in my_script.py and rerun 'run.sh' and now getting a new (strange) error:
Traceback (most recent call last):
[...]
File "/Users/my_computer/anaconda3/lib/python3.6/site-packages/tick/array/build/array.py", line 106
def tick_double_array_to_file(_file: 'std::string', array: 'ArrayDouble const &') -> "void":
^
SyntaxError: invalid syntax
First, check the python which you are calling the script with is pointing to the anaconda python and it is of the same version you are expecting it to be. You can do "which python" command in Linux and Mac to which the path which points to python. It if is pointing to some different version or build of python than the one which you are expecting then add the needed path to the system environment PATH variable. In Linux and Mac this can be done by adding the following line in the .bashrc file at the /home/ folder:
export PATH=/your/python/path:$PATH
And then source the .bashrc file.
source .bashrc
If you are on a operating system like cent os ,breaking the default python path can break your yum so be careful before changing it.
I am running a script in PyCharm and under the Project Interpretor I have the path
C:\envs\conda\keras2\python.exe
When I try to run the script via ssh on the server I get a 'no module named' error. I get
/usr/bin/python as the ans to 'which python' on the server itself. Could you tell me which path I must add for the script to run properly?
I have an annoying problem with PyCharm CE and naoqi. Following the installation instructions in Aldebaran Guide, I have configured my environment to work with this one.
I have installed the version of python (python 2.7.13) suggested by Aldebaran, I have written the .bash_profile using:
# Setting PATH for Python 2.7
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
#NaoQI 2.5.5
export PYTHONPATH=${PYTHONPATH}:/Users/francesco/Documents/PyLibs/pynaoqi-python2.7-2.5.5.5-mac64/lib/python2.7/site-packages
export DYLD_LIBRARY_PATH=${DYLD_LIBRARY_PATH}:/Users/francesco/Documents/PyLibs/pynaoqi-python2.7-2.5.5.5-mac64/lib
after that, I launch my code in python from Terminal (making sure that the right version of python is called when I type
$ python my_code.py
and all is working (I tried also with virtualenv and all works fine if I launch all from terminal).
So far so good but then comes the poison arrow. I use PyCharm CE like IDE, I have created a new project that use the same environment that works fine in terminal and the magic happens..
The first issue is:
/Users/francesco/Documents/PyEnv/pynaoqi255_python2713/bin/python /Users/francesco/PycharmProjects/PepperRobot/PepperRobot.py
Traceback (most recent call last):
File "/Users/francesco/PycharmProjects/PepperRobot/PepperRobot.py", line 5, in
import qi
ImportError: No module named qi
(Ok seriuosly this is the same environment that in terminal works)
I try to bypass the problem, adding the content of naoqi sdk in the virtualenv's site-package. Ok, the problem is bypassed.. But there is another problem:
/Users/francesco/Documents/PyEnv/pynaoqi255_python2713/bin/python /Users/francesco/PycharmProjects/PepperRobot/PepperRobot.py
Traceback (most recent call last):
File "/Users/francesco/PycharmProjects/PepperRobot/PepperRobot.py", line 5, in
import qi
File "/Users/francesco/Documents/PyEnv/pynaoqi255_python2713/lib/python2.7/site-packages/qi/init.py", line 88, in
from _qi import Application as _Application
ImportError: dlopen(/Users/francesco/Documents/PyEnv/pynaoqi255_python2713/lib/python2.7/site-packages/_qi.so, 2): Library not loaded: #loader_path/libqipython.dylib
Referenced from: /Users/francesco/Documents/PyEnv/pynaoqi255_python2713/lib/python2.7/site-packages/_qi.so
Reason: image not found
Process finished with exit code 1
I did not understand why...
I have also include in
Preferences -> console -> Python Console -> Environment Variables
the same variables included in .bash_profile but to no avail.
How can I solve this tedious problem ?
I have solved my problem editing run/debug configuration and putting here the environment variable DYLD_LIBRARY_PATH. All works fine.
I have been at this for a day and have found no solution. My python project is very levels deep and some of the modules have to be run independently (no -m flag, no relative imports). For this reason we decided to add the root folder to the PYTHONPATH. I am running OSX 10.11.3. Everything had been running smoothly until yesterday. I could run independent modules from the terminal with no issues in both Python 2.7 and 3.5. Yesterday, without modifying my PYTHONPATH or any other environment setting, running any of these independent modules from the terminal now gives me import errors in both Python 2 and 3.
Here is my working tree:
/Users/sintrafico/Documents/code/central_maestra
- reports_server.py
- api_server.py
- sintrafico
- sql
- SQLConnection
- api
- incident
- csv (not package)
- independent_module
- tests
I was running my tests yesterday with coverage with no problem, but now I can't.
Python 2 python csv/independent_module.py gives error:
Traceback (most recent call last):
File "csv/independent_module.py", line 13, in <module>
from sintrafico.sql import SQLConnection
ImportError: No module named sintrafico.sql
Python 3 python3 csv/independent_module.py gives error:
Traceback (most recent call last):
File "csv/create_bemobile_csv.py", line 13, in <module>
from sintrafico.sql import SQLConnection
ImportError: No module named 'sintrafico'
From within the incident folder, yesterday this command was working fine: coverage run -m unittest discover. Now all the tests fail because ImportError: No module named 'reports_server'.
Since that started happening yesterday, I have been messing with my PYTHONPATH. I left it blank and it didn't work (as expected), I have added the path now several ways:
export PYTHONPATH=“${PYTHONPATH}:/Users/sintrafico/Documents/code/central_maestra"
export PYTHONPATH=“/Users/sintrafico/Documents/code/central_maestra:${PYTHONPATH}"
export PYTHONPATH=“${PYTHONPATH}:/Users/sintrafico/Documents/code/central_maestra/"
export PYTHONPATH=“/Users/sintrafico/Documents/code/central_maestra:${PYTHONPATH}"
And also setting it without appending the PYTHONPATH since it was empty to begin with. I restarted my computer several times with no success. Also note, I have a .bash_profile and .bashrc with the following contents:
# Setting PATH for Python 3.5
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.5/bin:${PATH}"
export PATH
PYTHONPATH=“/Users/sintrafico/Documents/code/central_maestra:${PYTHONPATH}“
export PYTHONPATH
And lastly, what bugs me the most is that if I run the tests inside PyCharm they all run and pass, I get no import errors at all.
Have you tried setting the path without the quotes and braces? You are setting the python path as a string representation of the path instead of a list of paths