dod-cyber-crime-center / pyhidra

Pyhidra is a Python library that provides direct access to the Ghidra API within a native CPython interpreter using jpype.
Other
184 stars 16 forks source link

Cannot import name 'GhidraApplicationLayout' #45

Closed mixern6 closed 1 month ago

mixern6 commented 1 month ago

Pyhidra: 1.3.0 Python 3.12.6 OS: Windows 10

To reproduce:

import os
import pyhidra

# Set the Ghidra installation directory
os.environ['GHIDRA_INSTALL_DIR'] = "./ghidra/ghidra_11.2_PUBLIC"

pyhidra.start()

Error:

An error occured launching Ghidra: cannot import name 'GhidraApplicationLayout' from 'ghidra' (unknown location) Traceback (most recent call last): File "C:\project\test.py", line 7, in pyhidra.start() File "C:\Users\Dom\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyhidra\core.py", line 62, in start launcher.start() File "C:\Users\Dom\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyhidra\launcher.py", line 423, in start self._report_fatal_error("An error occured launching Ghidra", str(e), e) File "C:\Users\Dom\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyhidra\launcher.py", line 253, in _report_fatal_error raise cause File "C:\Users\Dom\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyhidra\launcher.py", line 420, in start self._pre_launch_init() File "C:\Users\Dom\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyhidra\launcher.py", line 319, in _pre_launch_init from ghidra import GhidraApplicationLayout ImportError: cannot import name 'GhidraApplicationLayout' from 'ghidra' (unknown location)

If the "ghidra" folder is renamed, there is no error

dc3-tsd commented 1 month ago

This is the expected behavior of python import (https://docs.python.org/3/tutorial/modules.html#packages).

You cannot have multiple modules/packages with the same name. When python searches your path it stops at the first hit it finds, in this case it is the original folder you named ghidra, and it stops progressing down through the other folders to find the actual ghidra module (and then does not find the files it needs). Unfortunately you cannot have packages (typically folders) and modules with the same name in your search path, so your best bet is to rename the parent folder to anything other than ghidra.

By default your current working directory is included in the search path (https://docs.python.org/3/library/sys_path_init.html).