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.
Related
I have a Fortran library compiled with gfortran into a DLL.
I am trying to call this DLL from python 3.9, and that can work, I can access the expected functions etc so this issue has nothing to do with ctypes etc.
Additionally, I know that the DLL works and can be called from python, another project uses the same DLL but has a flat folder structure (possibly to allow for this issue). However, I need it to be in a python package.
This main DLL has a few dependencies that need to be shipped with it. These dependencies must be in the parent directory of the main DLL (why I have no idea but that is the only way it works).
The issue occurs when trying to use this DLL in my python package.
If the entry point of the python code that calls the DLL is in the parent directory of the DLL then I can access the expected functions, if it is anywhere else I get the following error:
FileNotFoundError: Could not find module 'I:\<full-path>\wrappers\lib\foo.dll' (or one of its dependencies). Try using the full path with constructor syntax.
On the line:
self._libHandle = LoadLibrary(str(self._dll_path))
self._dll_path is a Path object with the absolute path to the DLL, I check the file exists before passing it to LoadLibrary.
I have the following directory structure (additional files removed for brevity):
src
|---entry.py
|---wrappers
| |---lib
| | |---foo.dll
| |---dep1.dll
| |---dep2.dll
| |---foo-wrapper.py
| |---adj-entry.py
If I add some test code to the bottom of foo-wrapper.py then I can access my DLL, if I import foo-wrapper into entry.py, I get the error above. Using the same code from entry.py in adj-entry.py works absolutely fine. The test code is shown below.
from src.wrappers import Foo
from pathlib import Path
dll_path = Path("../../src/wrappers/lib/foo.dll").resolve() # This path is the only thing adjusted between entry.py and adj-entry.py. Remove 1 ../ for entry.py
assert dll_path.exists()
assert dll_path.is_file()
f = Foo(dll_path)
What seems to be the only thing that changes is the file that python.exe is actually called with. When the file python is called with is in the DLL's parent directory everything works, if it is anywhere else I get the dependency error.
Does anyone know how I can call this DLL from anywhere?
Could this be related to the gfortran build or the Fortran code itself?
ran into a weird problem where there is a shared-object import error only when I run the script from command line. It succeed if i run the script in the python console using exec(...)
I have a class that needs a shared object: foo.py:
import os
cur_dir = os.curdir()
os.chdir('/tmp/dir_with_shared_object/')
import shared_object_class
os.chdir(cur_dir)
class Useful:
... # use the shared object import
Then there is a script like this:
from foo import Useful
If I enter python console and run:
exec(open('script.py').read())
Everything works fine.
If I run this on command line:
python script.py
I will get
ModuleNotFoundError: No module named 'shared_object_class'
The python is the same. It is 3.7.3, GCC 7.3.0 Anaconda. Anyone knows what is causing this discrepancy in behavior for shared object import?
A standard way of importing from a custom directory would be to include it in the PYTHONPATH environmental variable, with export PYTHONPATH=/tmp/dir_with_shared_object/.
update
It could also be done dynamically with
import sys
p = '/tmp/dir_with_shared_object/'
sys.path.insert(0, p)
PS
I think I have an explanation for why OP's original code didn't work. According to this python reference page, the import system searches, inter alia, in "[t]he directory containing the input script (or the current directory when no file is specified)." So the behavior in the REPL loop is different from how it is when running a script. Apparently the current directory is evaluated each time an import statement is encountered, while the directory containing the input script doesn't change.
I am trying to import a module from the prgoram called 'Power factory" in Python. The folder where power factory file located looks as follow:
I have written a script to import the powerfactory module as follow:
import sys
sys.path.append("PAth of folder")
import powerfactory as pf
When I ran the above code, it throws the following error:
ImportError: DLL load failed while importing powerfactory: The specified module could not be found.
I copied the .dll file present in the power factory folder into the Python DLL folder but no luck. Could anyone help me where am I making the mistake?
Searching the net I found this (from here)
I am not able to import powerfactory module: DLL load failed Category:
Scripting
If an error message appears when importing the powerfactory module
stating “ DLL load failed: the specified module could not be found”,
this means that Microsoft Visual C++ Redistributable for Visual Studio
2012 package is not installed on the computer.
To overcome this problem the user should add the PowerFactory
installation directory to the os path variable within his python
script.
import os
os.environ["PATH"] = r'C:\Program Files\DIgSILENT\PowerFactory 2016;' + os.environ["PATH"]
Copy the .dll file from your digsilent folder, eg. Program Files\DIgSILENT\PowerFactory 2020 SP2A\Python\3.8\boost_python38-vc141-mt-x64-1_68.dll
and place the .dll file directly in your system!
Save it to your C:\Windows\System32 folder.
&
Save it also to your C:\Windows\SysWOW64 folder.
You should be good to go.
There is a custom module "ETPython" generated by SWIG (consists of ETPython.py and binary _ETPython.so) and a simple script that invokes this module
sample.py
import ETPython
...
There aren't any problems if the script is run in an IDE (pycharm, internal python's IDLE, squish by froglogic so on). But if I'm trying to launch it in python shell in interactive mode or via terminal using
python3 sample.py
there is an error message like:
File "<path_to_file>/example.py", line 12, in <module>
import ETPython
File "<path_to_file>/ETPython.py", line 15, in <module>
import _ETPython
ImportError: dlopen(<path_to_file>/_ETPython.so, 2): Symbol not found: _NSLog
Referenced from: <path_to_file>/_ETPython.so
Expected in: flat namespace
Searching topics, I found that problem is related to wrong paths. So I added some code:
import os, sys
os.chdir("<path_to_dir>")
sys.path.append('<path_to_dir>')
os.system('export PYTHONPATH=<path_to_dir>:$PYTHONPATH')
This code helped to import the module in python shell in interactive mode but launching in terminal is still failing.
So the question is how to make it to work?
I found solution. The SWIG module was compiled incorrectly.
There was option for CMake that suppressed errors for undefined symbols
set(PLATFORM_LIBS "-undefined dynamic_lookup" )
That why the module doesn't contain NSLOG symbol.
The module was recompiled with additional
"-framework Foundation"
in swig_link_libraries statement. And now the module is imported correctly
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.