f2py does not properly import, despite successfully compiling - python-3.x

I am successfully compiling my Fortran subroutine with f2py. However, a folder is created as well as a pyd file.
If I do not put the created pyd file in the generated folder I get this error
ImportError: DLL load failed: The specified module could not be found.
If I put the generated .pyd file in the generated folder, my python script will run, and the import seems to work. However, if I run print(energy_f2py.__doc__) None is the output, and when I try to use the subroutine I get an error AttributeError: module 'energy_f2py' has no attribute 'potential_3'
potential_3 is the name of the subroutine inside energy_f2py.f90
I have also tried making energy_f2py.f90 a module containing the subroutine, with no difference in results.
This is the pyd file that is generated energy_f2py.cp36-win_amd64.pyd. I am using the commandline in Pycharm (windows 10 64 bit) to compile the Fortran file using
f2py -c -m energy_f2py energy_f2py.f90
python -m numpy.f2py -c -m energy_f2py energy_f2py.f90
The same thing happens.
It seems weird to me that both the pyd file is created as well as a folder, see the below image
I am stuck at this point... it seems like it is 95% of the way to working...
Lastly, this other post had a solution saying to name the Python and Fortran modules different names. I have tried this, and it did not make a difference either.

The best solution I found which worked was to partition my hardrive and install Linux. I chose to use Lubuntu, but you could use anything.
Everything works as it should on Linux.

Anther option to is to make it a static build, with the -static flag. This builds everything needed into a single file:
python -m numpy.f2py -c -m energy_f2py energy_f2py.f90 -static
You should then be able to use that .pyd file no matter what folder you've placed it in.

If you are using windows, I think adding --compiler=mingw32 may help. The defualt compiler is msvc on windows.

It occurs to me that the resulting .pyd module is looking for the DLL library in the same directory it itself resides in. Therefore, try moving the DLL from .libs directory to energy_f2py - it worked for me on Windows 10.

Related

Pyinstaller executable fails with pkg_resources.DistributionNotFound error

I'm making a PDF Tool executable using tkinter. Anyways, the executable was successfully created by pyinstaller, but it won't run. I flagged --onedir and added the necessary dependency files --add-data. I also added the paths to my non standard library packages using --paths flag. When I run the executable from the command prompt, I get this:
The problem appears to come from the ocrmypdf module and says pkg_resources.DistributionNotFound. I tried searching for the fix, but all the problems I saw were a bit different from my issue because the .py script runs just fine for me. Is this a pyinstaller issue, or am I missing a module? I'm using pyinstaller 4.0 as well.
After researching a little bit more, I've found the solution. The problem lies with pyinstaller, not the ocrmypdf module. The solution is that you have to create hook py scripts within a folder in your project. It's a little bit different depending on which module you use, but for this case, I had to create two hook py scripts within a folder that I called 'hooks'. These are the two scripts I made:
hook-ocrmypdf.py
from PyInstaller.utils.hooks import collect_all
datas, binaries, hiddenimports = collect_all('ocrmypdf')
hook-pikepdf.py
from PyInstaller.utils.hooks import collect_all
datas, binaries, hiddenimports = collect_all('pikepdf')
For more information go here.
Additionally, you must add the --additional-hooks-dir HookFolderPath flag to the pyinstaller command.

Can't get .exe to execute on other machine - "No module named serial" error

System: Python 3.7 64x running on Windows 10. I also have Anaconda installed on my machine and I program through VS Code. All modules have been installed, verified installed, and are up-to-date.
I have written a script and compiled it into an .exe using pyinstaller. pyinstaller -wFi pyico.ico endpointapp3.py.The .exe works and runs on my machine but it's not meant for my computer. Previous iterations of this script have been compiled, tested, and worked appropriately on its destination computer, a Windows 7 machine with no python installed.
My latest version now includes pyserial, and works, again, on my machine, but I after compiling and creating an .exe file for the destination machine I encounter this error.
File "endpointapp2.py", line 9 in <module>
ModuleNotFoundError: No module named 'serial'
[3512] Failed to execute script endpointapp2.py
Line 9 is my import serial line
As I mentioned before, this executable runs on my machine, but not on the destination machine it was designed for. I don't want to install python, or python modules on the destination machine because I want the .exe to be all inclusive.
Questions:
Why am I getting this error? What does it mean?
Is there a work around that allows me to compile everything together (using pyinstaller) that will hold all the necessary module
information within the .exe?
Is there something else I should be adding to my original script that will allow me to work through this?
NOT A DUPLICATE - This post is for compiling and running python .exe's on another machine, independent of python, with no modules or python installed.
With some assistance and helpful advice (thanks martineau) I was able to solve my problem. When compiling with pyinstaller, it makes a .spec file that it actually uses to compile to .exe.
Here I found the necessary steps to make sure I was adding the files I needed for pyinstaller.
Here I found out how to properly add the module information into my compiling code.
Once pyinstaller had the correct info in the .spec file I was able to compile and execute on the destination machine as intended.
I solve this issue when i move my code into serial module location.
First find where serial module locate :
>>> import serial
>>> serial.__file__
'C:\\Program Files\\Python310\\lib\\site-packages\\serial\\__init__.py'
mine is above.
Then move your file into :
'C:\\Program Files\\Python310\\lib\\site-packages\\{your_file.py}'
Then run pyinstaller :
pyinstaller --onefile your_file.py --additional-hooks-dir serial\\__init__.py
Run your .exe from dist folder. No longer showing error.
The module is called pyserial, but the library call in the code is import serial. Pyinstaller is unable to complete the build because there truly is no module named serial.
To correct this you need to tell pyinstaller to import the correct package by including it in the .spec file under hiddenimports.
hiddenimports=['pyserial']
This will be true for any case where the package name and the import statement call are not the same.

Why can't I compile a script that contains pyttsx3?

I have been working using pyttsx3 on win7 64bit python 3.4.3. Everything is good with the .py script I made using pyttsx3 module (works fine alone).
The problem comes when I compile it with pyinstaller. I got an error saying "pyttsx3.drivers not found" and I fixed that by using a .spec file with hiddenimports. Then I got an error that says pywintypes.com_error:(-2147352573,member not found,none,none).
I found on github that someone by the name #natambashat fixed it by commenting out the pyi-rth-win32comgenpy.py runtime hook because that's only needed for pyttsx. But that didnt work for me, I still get the same error. Can you please help me?
You cannot compile a script that contains pyttsx3 with pyinstaller because, pyinstaller like all compilers do not have every single python module. Here is a link that shows you all the compatible packages for pyinstaller.
Another option is py2exe. I am not a Windows user, so I don't know how to use it. But it supposedly supports pyttsx.
Another option though untested by me, is to download the pyttsx source code, put it into your project directory, import it in the code that uses pyttsx, then compile it with pyinstaller using:
pyinstaller --onefile app.py
Eureka!!i fixed it! Am gonna write what i did step by step in case some one come-across to the same problem.
1.i made a hook-pyttsx3.py file and commented out the pyi-rth-win32comgenpy.py file(have a look at this link of github.( instead of commenting out,you can also remove the line "win32:pyi-rth-win32comgenpy.py" from the rthooks.DAT file in ...Lib/sitepackages/pyinstaller/loader/rthooks).
2.go to ...Lib/sitepackages/Win32com/client/dynamic.py and to the _GetDeskInvokeType function.Replace the last line(return invoke_type) with return varkind.
->For some people just step 1 works,but for me i need to apply step2.I found somewhere in sourceforge(i dont remember the link),when compiling a script that involves win32,pywin32 version 221 and 220 gives the pywintypes error i mentioned above.But pywin32 version 219 doesnt give this error(i am using pywin32 v221).And the reason behind that is,in the function i mentioned in step2,return invoke_type is only on version 221 and 220 but the return on v219 is return varkind.I don't know why this differece came if it gives such errors!
EDIT:This link Is better than the above i gave.

Allow my friends to run my python script? [duplicate]

This question already has answers here:
Create a single executable from a Python project [closed]
(3 answers)
Closed 1 year ago.
I'm building a Python application and don't want to force my clients to install Python and modules.
So, is there a way to compile a Python script to be a standalone executable?
You can use PyInstaller to package Python programs as standalone executables. It works on Windows, Linux, and Mac.
PyInstaller Quickstart
Install PyInstaller from PyPI:
pip install pyinstaller
Go to your program’s directory and run:
pyinstaller yourprogram.py
This will generate the bundle in a subdirectory called dist.
pyinstaller -F yourprogram.py
Adding -F (or --onefile) parameter will pack everything into single "exe".
pyinstaller -F --paths=<your_path>\Lib\site-packages yourprogram.py
running into "ImportError" you might consider side-packages.
pip install pynput==1.6.8
still runing in Import-Erorr - try to downgrade pyinstaller - see Getting error when using pynput with pyinstaller
For a more detailed walkthrough, see the manual.
You can use py2exe as already answered and use Cython to convert your key .py files in .pyc, C compiled files, like .dll in Windows and .so on Linux.
It is much harder to revert than common .pyo and .pyc files (and also gain in performance!).
You might wish to investigate Nuitka. It takes Python source code and converts it in to C++ API calls. Then it compiles into an executable binary (ELF on Linux). It has been around for a few years now and supports a wide range of Python versions.
You will probably also get a performance improvement if you use it. It is recommended.
Yes, it is possible to compile Python scripts into standalone executables.
PyInstaller can be used to convert Python programs into stand-alone executables, under Windows, Linux, Mac OS X, FreeBSD, Solaris, and AIX. It is one of the recommended converters.
py2exe converts Python scripts into only executable on the Windows platform.
Cython is a static compiler for both the Python programming language and the extended Cython programming language.
I would like to compile some useful information about creating standalone files on Windows using Python 2.7.
I have used py2exe and it works, but I had some problems.
It has shown some problems for creating single files in Windows 64 bits: Using bundle_files = 1 with py2exe is not working;
It is necessary to create a setup.py file for it to work. http://www.py2exe.org/index.cgi/Tutorial#Step2;
I have had problems with dependencies that you have to solve by importing packages in the setup file;
I was not able to make it work together with PyQt.
This last reason made me try PyInstaller http://www.pyinstaller.org/.
In my opinion, it is much better because:
It is easier to use.
I suggest creating a .bat file with the following lines for example (pyinstaller.exe must be in in the Windows path):
pyinstaller.exe --onefile MyCode.py
You can create a single file, among other options (https://pyinstaller.readthedocs.io/en/stable/usage.html#options).
I had only one problem using PyInstaller and multiprocessing package that was solved by using this recipe: https://github.com/pyinstaller/pyinstaller/wiki/Recipe-Multiprocessing.
So, I think that, at least for python 2.7, a better and simpler option is PyInstaller.
And a third option is cx_Freeze, which is cross-platform.
pyinstaller yourfile.py -F --onefile
This creates a standalone EXE file on Windows.
Important note 1: The EXE file will be generated in a folder named 'dist'.
Important note 2: Do not forget --onefile flag
You can install PyInstaller using pip install PyInstaller
NOTE: In rare cases there are hidden dependencies...so if you run the EXE file and get missing library error (win32timezone in the example below) then use something like this:
pyinstaller --hiddenimport win32timezone -F "Backup Program.py"
I like PyInstaller - especially the "windowed" variant:
pyinstaller --onefile --windowed myscript.py
It will create one single *.exe file in a distination/folder.
You may like py2exe. You'll also find information in there for doing it on Linux.
Use py2exe.... use the below set up files:
from distutils.core import setup
import py2exe
from distutils.filelist import findall
import matplotlib
setup(
console = ['PlotMemInfo.py'],
options = {
'py2exe': {
'packages': ['matplotlib'],
'dll_excludes': ['libgdk-win32-2.0-0.dll',
'libgobject-2.0-0.dll',
'libgdk_pixbuf-2.0-0.dll']
}
},
data_files = matplotlib.get_py2exe_datafiles()
)
I also recommend PyInstaller for better backward compatibility such as Python 2.3 - 2.7.
For py2exe, you have to have Python 2.6.
For Python 3.2 scripts, the only choice is cx_Freeze. Build it from sources; otherwise it won't work.
For Python 2.x I suggest PyInstaller as it can package a Python program in a single executable, unlike cx_Freeze which outputs also libraries.
Since it seems to be missing from the current list of answers, I think it is worth mentioning that the standard library includes a zipapp module that can be used for this purpose. Its basic usage is just compressing a bunch of Python files into a zip file with extension .pyz than can be directly executed as python myapp.pyz, but you can also make a self-contained package from a requirements.txt file:
$ python -m pip install -r requirements.txt --target myapp
$ python -m zipapp -p "interpreter" myapp
Where interpreter can be something like /usr/bin/env python (see Specifying the Interpreter).
Usually, the generated .pyz / .pyzw file should be executable, in Unix because it gets marked as such and in Windows because Python installation usually registers those extensions. However, it is relatively easy to make a Windows executable that should work as long as the user has python3.dll in the path.
If you don't want to require the end user to install Python, you can distribute the application along with the embeddable Python package.
py2exe will make the EXE file you want, but you need to have the same version of MSVCR90.dll on the machine you're going to use your new EXE file.
See Tutorial for more information.
You can find the list of distribution utilities listed at Distribution Utilities.
I use bbfreeze and it has been working very well (yet to have Python 3 support though).
Not exactly a packaging of the Python code, but there is now also Grumpy from Google, which transpiles the code to Go.
It doesn't support the Python C API, so it may not work for all projects.
Using PyInstaller, I found a better method using shortcut to the .exe rather than making --onefile. Anyway, there are probably some data files around and if you're running a site-based app then your program depends on HTML, JavaScript, and CSS files too. There isn't any point in moving all these files somewhere... Instead what if we move the working path up?
Make a shortcut to the EXE file, move it at top and set the target and start-in paths as specified, to have relative paths going to dist\folder:
Target: %windir%\system32\cmd.exe /c start dist\web_wrapper\web_wrapper.exe
Start in: "%windir%\system32\cmd.exe /c start dist\web_wrapper\"
We can rename the shortcut to anything, so renaming to "GTFS-Manager".
Now when I double-click the shortcut, it's as if I python-ran the file! I found this approach better than the --onefile one as:
In onefile's case, there's a problem with a .dll missing for the Windows 7 OS which needs some prior installation, etc. Yawn. With the usual build with multiple files, no such issues.
All the files that my Python script uses (it's deploying a tornado web server and needs a whole freakin' website worth of files to be there!) don't need to be moved anywhere: I simply create the shortcut at top.
I can actually use this exact same folder on Ubuntu (run python3 myfile.py) and Windows (double-click the shortcut).
I don't need to bother with the overly complicated hacking of .spec file to include data files, etc.
Oh, remember to delete off the build folder after building. It will save on size.
Use Cython to convert to C, compile, and link with GCC.
Another could be, make the core functions in C (the ones you want to make hard to reverse), compile them and use Boost.Python to import the compiled code (plus you get a much faster code execution). Then use any tool mentioned to distribute.
I'm told that PyRun is also an option. It currently supports Linux, FreeBSD and Mac OS X.

julia: system image file "sys.ji" not found

I am using the IDE Netbeans to code a project c++ under Linux(red hat 7). As I need to use some math functions, I try to embed the julia language into my c++ project.
Here is what I have done:
Download the Julia from here (I choose this: Generic Linux binaries)
Set project properties: build-->C++ Complier-->Include Directories, add the include of Julia, something like this: ../myjulia/include/julia
Add the libraries: open a terminal and type the command line: sudo ln -s ../myjulia/lib/julia/libjulia.so /usr/lib/libjulia.so
Now if I run my project, I will get this error: System image file "../myproject/dist/Debug/GNU-Linux-x86/../lib/julia/sys.ji" not found
I hve checked this file: ../myjulia/lib/julia, in this file, there are all of the lib files (libjulia.so etc) and a file named "sys.ji".
I ran into this issue after installing Julia v0.3.10 on a Windows machine and thought I'd post it in case it can help someone else.
When I ran julia.exe it gave me the error message listed above.
Problem:
I had created a system environment variable called JULIA_HOME and pointed it to the directory where Julia was installed. Then, I added %JULIA_HOME%\bin to my PATH.
Solution:
I pointed JULIA_HOME to the \bin directory under the Julia install directory. Then, I added %JULIA_HOME% to my PATH
A "hello world" example from here
Now we know that we need to setup the julia context with this code:
jl_init(NULL);
In fact this code may not setup a good context because the project can't find the system image file "sys.ji". So what we need to do is using another function instead of jl_init: jl_init_with_image. This function accept two parameters: the first is the path of the image file, the second is the name of the image file. So we should use it like this: jl_init_with_image("/thePathOfSys.ji", "sys.ji"); One more thing: the path of sys.ji must be the absolute path.

Resources