# Process Create Path of Executable

**URL:** <https://swi-prolog.discourse.group/t/process-create-path-of-executable/3613>\
**Category:** Help!\
**Created:** [February 16, 2021, 8:02pm UTC](https://swi-prolog.discourse.group/t/process-create-path-of-executable/3613 "2021-02-16T20:02:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![aminemarref](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@aminemarref](https://swi-prolog.discourse.group/u/aminemarref)\
**Post date:** [February 16, 2021, 8:02pm UTC](https://swi-prolog.discourse.group/t/process-create-path-of-executable/3613/1 "2021-02-16T20:02:00Z")

</div>

Hello,

I use [process\_create/3](https://www.swi-prolog.org/pldoc/man?predicate=process_create/3) to communicate with Python scripts.

I call the predicate this way:

```prolog
process_create(
   path(python),
   [SymPyScript],
   [stdout(pipe(OSTerminalOutputStream))]
).

```

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.

Many thanks.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [February 16, 2021, 8:30pm UTC](https://swi-prolog.discourse.group/t/process-create-path-of-executable/3613/2 "2021-02-16T20:30:01Z")

</div>

> [@aminemarref](#):
>
> What is exactly `path(python)` ?

This says that you want to use the command `python` and you want Prolog to find it based on SWI-Prolog knowledge of file paths, e.g.

```prolog
?- file_search_path(path,Path).

```

> [@aminemarref](#):
>
> How is it set?

If you look at the top of the documentation for [library(process): Create processes and redirect I/O](https://www.swi-prolog.org/pldoc/man?section=process) it notes

In addition to the predicates, this module defines a file search path (see user:file\_search\_path/2 and [absolute\_file\_name/3](https://www.swi-prolog.org/pldoc/man?predicate=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` :

```prolog
?- absolute_file_name(path(ls), Path, [access(execute)]).

```

* * *

I suspect that

```prolog
?- absolute_file_name(path(python),Path).

```

will work on Windows and fail on Linux.

HTH

* * *

**EDIT**

As Jan W. points out [below](https://swi-prolog.discourse.group/t/process-create-path-of-executable/3613/4), my intent in writing this was to suggest that on your machine you might try to use

```prolog
?- 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.

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [February 17, 2021, 12:29am UTC](https://swi-prolog.discourse.group/t/process-create-path-of-executable/3613/3 "2021-02-17T00:29:35Z")

</div>

> [@aminemarref](#):
>
> Does it copy the variable PYTHONHOME in Ubuntu for example

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:

```prolog
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`):

```prolog
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.,

> <https://stackoverflow.com/questions/3781522/why-do-python-modules-sometimes-not-import-their-sub-modules/3781554#3781554>

> <https://stackoverflow.com/questions/2724260/why-does-pythons-import-require-fromlist/2725668#2725668>

> **[Human verification](https://stackoverflow.com/nocaptcha?s=354ed34c-c0f1-4e51-a17b-1fbbdc9c5740)**
>
> Stack Overflow | The World’s Largest Online Community for Developers

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [February 17, 2021, 7:55am UTC](https://swi-prolog.discourse.group/t/process-create-path-of-executable/3613/4 "2021-02-17T07:55:11Z")

</div>

> [@EricGT](#):
>
> I suspect that
> 
> ```prolog
> ?- absolute_file_name(path(python),Path).
> 
> ```
> 
> will work on Windows and fail on Linux.

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).

---

<div class="post-metadata">

**Author:** ![aminemarref](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@aminemarref](https://swi-prolog.discourse.group/u/aminemarref)\
**Post date:** [February 17, 2021, 8:27am UTC](https://swi-prolog.discourse.group/t/process-create-path-of-executable/3613/5 "2021-02-17T08:27:22Z")

</div>

> [@peter.ludemann](#):
>
> `sys.path.insert(0, os.path.dirname(sys.argv[0]))`

This is for me more of a blackhole than black art 🙂  
I tried playing along your suggestions but my technical (dirty) knowledge approaches zero.

I found this juicy piece of evidence though: this is the error I get when my Prolog program fails.

> ImportError: No module named sympy  
> ERROR: -g prove(‘/home/amine/problems/’,‘problem.txt’): Process “/usr/bin/python”: exit status: 1

So I guess this means that Prolog is looking for the python inside /usr/bin

and this is my path:

> $ echo $PATH  
> /home/amine/tools/ext/anaconda3/bin:/home/amine/tools/ext/anaconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

The python that should be invoked is the python in anaconda:

> $ which python  
> /home/amine/tools/ext/anaconda3/bin/python

but the one invoked is:

> /usr/bin/python

Any ideas?

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [February 17, 2021, 9:11am UTC](https://swi-prolog.discourse.group/t/process-create-path-of-executable/3613/6 "2021-02-17T09:11:21Z")

</div>

> [@aminemarref](#):
>
> Any ideas?

Bit weird. Prolog is indeed started with this environment? You can check how Prolog interprets `$PATH` using this, enumerating all solutions.

```
?- file_search_path(path, X).

```

This should also succeed:

```
?- access_file('/home/amine/tools/ext/anaconda3/bin/python', execute).

```

You can also set this to see what is being tried when searching for a file:

```
?- set_prolog_flag(verbose_file_search, true).

```

And there is this to verify Prolog agrees on `$PATH`

```
?- getenv('PATH', X).

```

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [February 17, 2021, 6:35pm UTC](https://swi-prolog.discourse.group/t/process-create-path-of-executable/3613/7 "2021-02-17T18:35:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![aminemarref](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@aminemarref](https://swi-prolog.discourse.group/u/aminemarref)\
**Post date:** [February 18, 2021, 9:59am UTC](https://swi-prolog.discourse.group/t/process-create-path-of-executable/3613/8 "2021-02-18T09:59:26Z")

</div>

It was Anaconda messing my stuff, I installed SymPy not using Anaconda and it worked. Thanks everyone for your help.

With all the Anaconda and Python things, I have developed serious Ophidiophobia.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [February 18, 2021, 1:51pm UTC](https://swi-prolog.discourse.group/t/process-create-path-of-executable/3613/9 "2021-02-18T13:51:07Z")

</div>

> [@aminemarref](#):
>
> [Anaconda](https://www.anaconda.com/)

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](https://code.visualstudio.com/docs/remote/remote-overview).

I feel your pain!
