This works fine for Windows, but in Linux I get an error that Python cannot find an imported module included in the script SymPyScript.
What is exactly path(python)? How is it set? Does it copy the variable PYTHONHOME in Ubuntu for example? I am asking this because I have a few Python versions in my system, and one of them is included in Anaconda and contains the imports I need.
In addition to the predicates, this module defines a file search path (see user:file_search_path/2 and absolute_file_name/3) named path that locates files on the systemâs search path for executables. E.g. the following finds the executable for ls :
As Jan W. points out below, my intent in writing this was to suggest that on your machine you might try to use
?- absolute_file_name(path(python),Path).
as a test to disseminate where the problem lies. The way the accompanying text reads, it implies that this will always fail on Linux which I know is clearly not true.
PYTHONHOME, PYTHONPATH, etc. are Python-specific, and SWI-Prolog knows nothing about them ⌠absolute_file_name/3 is used to resolve a file specification (such as path(python)) to a file name.
I suspect that something is going on with the âcurrent directoryâ being different in Windows and Ubuntu. To figure this out, you could try this Python program:
import os, sys
print(sys.argv)
print(os.path.dirname(sys.argv[0])) # directory part of the executable
print(sys.path)
This should show you the result of applying PYTHONHOME, PYTHONPATH, etc. If sys.path doesnât contain your moduleâs path, you can try something like the following (gets the executable name from sys.argv, puts its directory first in sys.path):
sys.path.insert(0, os.path.dirname(sys.argv[0]))
On my system, it seems that the Python executable puts the executableâs directoyr first; if your module is in some other place, you might need to do something like sys.path.insert(0, os.path.join(os.path.dirname(sys.argv[0]), 'my_module_dir').
Resolving Python modules (and packages, which are not quite the same thing) is a bit of a black art - e.g., be sure you have __init__.py files (even if empty). If youâre looking in stackoverflow, Thomas Wouters is authoritative (user:17624). E.g.,
Note that Python seems to be found and started. create_process/3 uses absolute_file_name/3 with the option file_type(executable) as well, which notably make it try and add the Windows executable extensions.
The OPâs problem is most likely with the environment variables (which are simply passed if noting is specified) or the working directory (which is also unchanged unless specified).
Iâve found that anaconda does weird things to the environment â for example, it made tkdiff use weird fonts. Maybe try conda deactivate and see what happens?
(Also, python might resolve do a different version on your two systems; I like to specify the version, e.g. python3.7).
If youâre using Python 2, I recommend switching to Python 3. It allows more options with import, such as from ..someDir import foo. I was able to resolve my module-loading problems by using the from . import style.
When I transitioned to doing some serious coding with Python I installed Anaconda and then just started regretting it more and more. Granted Anaconda is great if you buy-in into the entire philosophy but it wrecked havoc with my normal means of work.
The next time I re-image my machine, Anaconda is not getting installed on the base machine. If I do need it it will probably be put/accessed as a Python development container for use with VSCode remote development.