ImportError: No module named in IDA script - python

I executed a IDA test.py script on Ubuntu with this command './idal -S"test.py" -t',
there would be a error - no module named xxx, i imported xxx module in this script, but when i separately executed test.py, it is OK.
I do not know why this happened?
why it can not find xxx module?
but when i execute it under Windows version IDA, it is OK.

Try placing your test.py in another directory. The file python.cfg in the cfg folder in the ida directory contains the following by default:
// Remove current directory from import search path
REMOVE_CWD_SYS_PATH = 1
Please let me know if this helped.

Related

Python3 ModuleNotFoundError when running from command line but works if I enter the shell

I think I'm missing something obvious here. I cloned this repo, and now have this directory structure on my computer:
When I try to run python baby_cry_detection/pc_main/train_set.py, I get a ModuleNotFoundError.
Traceback (most recent call last):
File "baby_cry_detection/pc_main/train_set.py", line 10, in <module>
from baby_cry_detection.pc_methods import Reader
ModuleNotFoundError: No module named 'baby_cry_detection'
However, if I type python and enter the interactive shell and then type the command
from baby_cry_detection.pc_methods import Reader
it imports just fine with no error. I'm completely baffled. I'm using a virtualenv and both instances are using the same python installation, and I haven't changed directories at all.
I think sys.path could be the reason that the module is not found when python command is executed. Here is how we can check if that is indeed the case:
In the train_set.py file, add import sys; print(sys.path). Looking at the error, the path may contain /path/to/baby_cry_detection/baby_cry_detection/pc_main. If that is the case, then we have found the issue which is that baby_cry_detection.pc_methods will not be found in the directory that sys.path is looking into. We'll need to append the parent baby_cry_detection directory to sys.path or use relative imports. See this answer.
The reason that python prompt successfully imports the module could be because the prompt is started in the correct parent directory. Try changing the directory to baby_cry_detection/pc_main/ and try importing the module.

Trying to execute a module within a package downloaded from github gets error

When I try to run the main script file of a Python package, from Windows cmd prompt, I get Python errors.
I downloaded a folder from github, that contains a multi-folder Python package, to a folder zzz\ on my Windows 10 computer. The package's top folder is OJWALCH_sleep_classifiers.
To run the package, I must run this module:
zzz\OJWALCH_sleep_classifiers\source\preprocessing\preprocessing_runner.PY
I can't figure out how without error.
I opened a cmd prompt and did cd into zzz\OJWALCH_sleep_classifiers.
Here is what I've tried:
1st attempt...
At cmd line: python -m path and name of module
python -m source\preprocessing\preprocessing_runner
ERROR:
C:\Users\Doug\AppData\Local\Programs\Python\Python37\python.exe: No module named source\preprocessing\preprocessing_runner
2nd attempt...
At cmd line: python <path and name of module>.py
python
python source\preprocessing\preprocessing_runner.PY
ERROR:
Traceback (most recent call last):
File "source\preprocessing\preprocessing_runner.PY", line 3, in <module>
from source.analysis.figures.data_plot_builder import DataPlotBuilder
ModuleNotFoundError: No module named 'source'
3rd attempt... CAN'T FIND MODULE: preprocessing_runner
At cmd line: python <path and name of module>
python source\preprocessing\preprocessing_runner
ERROR:
(null): can't open file 'source\preprocessing\preprocessing_runner': [Errno 2] No such file or directory
At cmd line: python -m path and name of module
This way, because you are giving the name directly to Python, you should use . between folder names, and no file name extension - because you are naming a package and module, not a path and file. So:
python -m source.preprocessing.preprocessing_runner
At cmd line: python .py
This would have worked, except that you tried to start Python first, and then give a command-line command to Python, instead of giving it to the command line.
At cmd line: python
Since this is a single command that starts up Python, we are giving it a path and file. So there should now be a .py extension:
python source\preprocessing\preprocessing_runner.py
We can also use forward slashes at the command prompt - it will break tab-completion, though.

Powershell scripts doesn`t function well when running in Python

when I was in Powershell interface, I ran these command:
PS C:\Users\administrator.HYPERV> Import-Module FailoverClusters
PS C:\Users\administrator.HYPERV> get-module -listAvailable
I can see module FailoverClusters in result.
But when I ran Python code:
>>> os.system(r"powershell import-module FailoverClusters")
It gives me this:
import-module:The specified module "FailoverClusters" was not loaded
because no valid module file was found in any module directory
and then I ran
>>> os.system(r"powershell get-module -listAvailable")
module FailoverClusters didn`t show in the result.
How can I get these python code work as I expected?

ImportError in Scripts directory

Using windows 7 and python 2.7. I have a package named Regetron in c:\Python27\Lib\site-packages\regetron which contains __init__.py and engine.py. When I try to run this library from the command prompt by typing regetron I get the following error:
Traceback (most recent call last):
File "C:\Python27\Scripts\regetron.py", line 6, in <module>
from regetron.engine import Regetron
File "C:\Python27\Scripts\regetron.py", line 6, in <module>
from regetron.engine import Regetron
ImportError: No module named engine
I added c:\Python27\Lib\site-packages\regetron to %PYTHONPATH% and can successfully import this module from other scripts located in other folders as well as the interactive prompt, but for some reason it refuses to run from the command prompt. What is going on?
You actually have two problems here. Fixing either one of them would actually eliminate your immediate error, but you need to fix both of them.
When I try to run this library from the command prompt by typing regetron
You shouldn't have a script named regetron and also have a module or package named regetron. Fix it by renaming your script. But if you want to understand why it's causing a problem:
The current working directory is always part of sys.path. So, you're in the directory with regetron.py in, and you run it with regetron. That means that regetron.py is on the path. So when you import regetron, it finds your script, not the package. Or, when you from regetron import engine, it finds your script, and tries to import a variable/function/class/whatever named engine from it, rather than finding the package and trying to import a module underneath it.
I added c:\Python27\Lib\site-packages\regetron to %PYTHONPATH%
Never add a package's directory to sys.path.
Since site-packages is already on your sys.path, the code in regetron/engine.py is already available as regetron.engine. You don't want it to also be available as engine. This will cause all kinds of problems.
So, rename you script to something else, remove regetron from %PYTHONPATH%, and everything will be fine.
But you may want to (re-)read the section on Packages in the tutorial.

Creating and using of procname.so

I would like to edit the process name of my python scripts by the use of http://code.google.com/p/procname/
I downloaded the source file of procname, extracted the content into my script directory and changed the makefile to:
PYTHON?=python3
TESTFLAGS=-p -v
TESTOPTS=
SETUPFLAGS=
GCC=gcc
VER=3.1
DESTDIR=.
Then I did make and procname.so was created in the same directory where my script is.
In the script I added import procname and tried to edit the process name by procname.setprocname('test_name.py').
Now i get an error:
import procname
ImportError: /path/to/script_directory/procname.so: undefined symbol: Py_InitModule3
The procname extension module does not work in Python 3. procnamemodule.c uses Py_InitModule3, which no longer exists in Python 3.

Categories