Module ImportError in running Python Script in JMeter - python

I would like to use python scripts in Jmeter, so I can check the Load and the performance of an application...for that I've created a Thread with JSR223 Sampler, and I put the content of python script, and after the run, Jmeter throws error like : File "", line 8, in
ImportError: No module named opcua, ModuleNotFoundError: No module named 'dateutil'
see snapshot:
I don't know how to install this modules (package)in Jmeter, with PyCharm, I installed the package in the Project Interpreter under the setting see the snapshot...

You can do something like:
Install required module(s)
jython -m pip install foo
Add the next line(s) to the beginning of your script:
import sys
sys.path.append('\\location\\of\\jython\\site-packages')
However from performance perspective it will be much better to re-write your script in Groovy as calling Jython interpreter is not the best choice when it comes to high loads.
Also be aware that you can invoke Python interpreter as "external" process using JMeter's OS Process Sampler (if amount of the existing Python code is too big or too complex to rewrite in Groovy), see How to Run External Commands and Programs Locally and Remotely from JMeter for more details.

Related

Accessing system modules from vs code python interactive window

I am using the VS code interactive window and using some python packages that make use of other programs. In the interactive window I am e.g. getting error messages like:
OSError: No command "mcflirt" found on host login2.nan.kcl.ac.uk. Please check that the corresponding package is installed.
If I was just running this from the command line I would simply ensure that I loaded the package which contains 'mcflirt' into the shell before starting the python interpreter and the interpreter would then be able to find it. Is anyone aware of how to tell the interactive window to load the package (e.g. loading the mcflirt containing package by module load fsl)before starting the python interpreter?
One way to access system modules is to use the python 'init' file e.g.:
sys.path.insert(0,'/software/system/modules/4.5.1/init')
import python as mod
mod.module('unload', 'fsl')
mod.module('load', 'fsl/6.0.4')
mod.module('load', 'ants/2.3.5')

Using python subprocess with module load

I'm currently using Python 2.7 on a unix environment.
I need to run R scripts in my python scripts but I can't manage to make it work because my R module needs to be loaded before (using "module load")
Here's my python script :
import os
import subprocess as sp
os.system('module load R/3.2.3')
out = sp.check_output(['Rscript','test.R'], universal_newlines=True)
I keep having the same error : "[Errno 2] No such file or directory"
Any idea ?
I looked here and here but couldn't make it work.
Thank you for your help !
So what "module load" actually does is set some environment variables in the calling shell. So when you do this:
os.system('module load R/3.2.3')
Python creates a process, runs /bin/sh in it, and passes that command to the shell. The module environment variables are set in that shell. Then that shell exits--job done!
The environment variables do not--and cannot--propagate back to the Python process. So when you do this:
sp.check_output(['Rscript','test.R'])
It's totally irrelevant that you ran module load before.
So how can you fix this? Well, one possibility would be to explicitly specify the path to Rscript:
sp.check_output(['/your/full/path/to/Rscript','test.R'])
Another would be to combine your commands:
sp.check_output('module load R/3.2.3 && Rscript test.R', shell=True)
Finally, you could simply run module load before running your Python script in the first place. The environment variables it sets can propagate all the way to the R invocation within Python.
By the way, it is possible to invoke R directly from Python: http://rpy.sourceforge.net/rpy2/doc-dev/html/introduction.html

Python pdb on python script run as package

I have a python program that I usually run as a part of a package:
python -m mymod.client
in order to deal with relative imports inside "mymod/client.py." How do I run this with pdb - the python debugger. The following does not work:
python -m pdb mymod.client
It yields the error:
Error: mymod.client does not exist
EDIT #1 (to address possible duplicity of question)
My question isn't really about running two modules simultaneously python, rather it is about how to use pdb on a python script that has relative imports inside it and which one usually deals with by running the script with "python -m."
Restated, my question could then be, how do I use pdb on such a script while not having to change the script itself just to have it run with pdb (ie: preserving the relative imports inside the script as much as possible). Shouldn't this be possible, or am I forced to refactor in some way if I want to use pdb? If so what would be the minimal changes to the structure of the script that I'd have to introduce to allow me to leverage pdb.
In summary, I don't care how I run the script, just so long as I can get it working with pdb without changing it's internal structure (relative imports, etc) too much.
I think I have a solution.
Run it like this:
python -m pdb path/mymod/client.py arg1 arg2
that will run it as a script, but will not treat it as a package.
At the top of client.py, the first line should be:
import mymod
That will get the package itself loaded.
I am still playing with this, but it seems to work so far.
This is not possible. Though unstated in documentation, Python will not parse two modules via the -m command line option.

GNU module load before loading python module

On a server I am working on, I need to run the following commands to ensure the xlsxwriter is available to import from python:
module load swdev
module load python/xlsxwriter_py3.4.2/0.7.2
However, I would like this to be done automatically when the python script that needs it is run, from within the python script. Running os.system or subprocess.call doesn't work. How do I do this?
You can call module from a Python script. The module command is provided by the environment-modules software, which also provides a python.py initialization script.
Evaluating this script in a Python script enables the module python function. If environment-modules is installed in /usr/share/Modules, you can find this script at /usr/share/Modules/init/python.py.
Following code enables module python function:
import os
exec(open('/usr/share/Modules/init/python.py').read())
Thereafter you can load your modules:
module('load', 'swdev')
module('load', 'python/xlsxwriter_py3.4.2/0.7.2')

Can't get Python IDLE to recognize OGR/GDAL Module

Folks,
Just getting started using OGR and Python for a variety of geospatial tasks. I'm working outside of OSGEO4w, and have installed GDAL w/ Python Bindings as well as Python v. 2.7.8 on my machine.
That said, I can run python and import gdal from a command-line interface but am unable to import the module when I use the IDLE environment. It returns an error telling me that the module doesn't exist. My install must be sound given that it works in the cmd prompt, so what's the deal?
This is all new to me, I'd appreciate any help or advice ya'll can impart; all previous questions I've read were concerned w/ merely installing (which I've done successfully) and getting Python to recognize the module from the command prompt, which it does.
Cheers,
Mike
Mike,
You can open IDLE with your python installation from the cmd line through:
>>>from idlelib import PyShell
>>>PyShell.main()
This should open IDLE from your current python, and you should be able then to import gdal and ogr from there.
Alternatively, you should have a windows batch file here
C:\YOURPYTHONPATH\Lib\idlelib\idle.bat
Running this should achieve the same.
Martin
Consider creating the following batch where PyInst references the Python installation folder from which IDLE is launched (i.e. idle.bat) and QgisInst references the folder containing "bin\o4w_env.bat":
#echo off
set PyInst=C:\Python27
set QgisInst="C:\Program Files\QGIS Brighton"
call %QgisInst%\bin\o4w_env.bat
set PYTHONPATH=%PyInst%\DLLs;%PyInst%\Lib;%PyInst%\Lib\lib-tk
set TCL_LIBRARY=%PyInst%\tcl\tcl8.5
python %PyInst%\Lib\idlelib\idle.pyw

Categories