I want to specify to Forpy which python to use but I can’t figure it out. The only hack I found is to make a python virtual environment so that the default python is changed inside of the environment but this is not good enough for my case.
PS: I also asked the question in the Forpy repo. I’m asking it here too so that I can reach more people.
In Python you either have to use an environment, or you install manually into a given Python. You change the default Python by putting the correct python in your $PATH. The first line in the script:
Looks up the python in your $PATH and uses that. So just put python version 3.9 (that you need) in your $PATH and that should do it. Alternatively you can specify a full (absolute) path to it on that first line.
I tried that out but afaik that file is not really necessary. FORPY’s author writes in the readme that you only really need the file forpy_mod.F90. I could pinpoint my issue a bit more. Here is a Fortran program that starts the Python interpreter, imports sys and prints the Python executable aswell as the Python version it is using.
program print_python_version
use forpy_mod
implicit none
integer :: ierror
type(module_py) :: sys
type(object) :: version, executable
character(len=:), allocatable :: version_str, executable_str
ierror = forpy_initialize()
ierror = import_py(sys, "sys")
ierror = sys%getattribute(version, "version")
ierror = sys%getattribute(executable, "executable")
ierror = cast(version_str, version)
ierror = cast(executable_str, executable)
call forpy_finalize()
print *, "Python VERSION is ", version_str
print *, "Python EXECUTABLE is ", executable_str
end program
Python VERSION is 3.8.10 (default, Sep 11 2024, 16:02:53)
[GCC 9.4.0]
Python EXECUTABLE is /usr/bin/python3
If I link with python3.9 libraries instead by changing to python3.9-config I get the following output:
Python VERSION is 3.9.10 (main, May 16 2022, 19:45:25)
[GCC 9.4.0]
Python EXECUTABLE is /usr/bin/python3
What I think is happening is it does manage to import the sys module of my python3.9 but it is using the wrong python executable. I am stuck here for now.
I made a Docker image with two pythons (3.8 and 3.9) where you can try out and see the issue for yourself if anyone wants to give it a shot. If you have docker and you are on a Unix machine you can do:
The only solution I found so far is to make a virutal environment so that inside the virtual environment the default python is changed. This way Forpy uses the python of the venv, and even if the python3.9-config script is not in the virtual environment folder, it seems to work fine.
You could also consider using one of the various flavors of conda (anaconda, miniconda, I’ve heard mamba is popular lately) to grab specific versions of Python other than your system install, since going off the name, your script is tailored to a specific Python version that you also coincidentally happen to have on your system.
I’m generally not wild about conda because I’ve had its excessive environment pollution screw me over more than once, but I haven’t found anything better to deal with how “foamy” the Python ecosystem is. (It’s one of my biggest annoyances using a bleeding edge Linux distro as my daily driver, it’s a miracle whenever any Python script longer than “Hello World” actually works.)