Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adder.py needs to be in the current working directory #2

Closed
UnixJunkie opened this issue Jan 28, 2022 · 8 comments
Closed

adder.py needs to be in the current working directory #2

UnixJunkie opened this issue Jan 28, 2022 · 8 comments

Comments

@UnixJunkie
Copy link

or you get this kind of error:

Fatal error: exception E (<class 'ModuleNotFoundError'>, No module named 'adder')

Is there a way I can install this python file somewhere else, but so that
it could still be found at runtime?

@mooreryan
Copy link
Owner

Hey, this is a good question. I'm working on adding a couple of examples to the repo to show how to do this correctly. I will update you when they are ready!

@mooreryan
Copy link
Owner

The short answer is that you could set the PYTHONPATH environmental variable to wherever the python module you want to bind is located. Or you can "install" the python package in the typical python way.

The longer answer....So I added some examples that may help you out. This one in particular has some modules that it's importing (https://github.com/mooreryan/ocaml_python_bindgen/tree/main/examples/importing_modules). The readme in there is fairly detailed, so hopefully it will help. Oh and as a bonus, the dune files shows how to set up dune rules to automatically generate the bindings when your specs are updated.

If you need more help, let me know. Otherwise, feel free to close the issue!

@UnixJunkie
Copy link
Author

Better answer, I asked to @thierry-martinez:
thierry-martinez/pyml#77
So, simple answer,

let toto = Py.Import.import_module "toto"

Now, combine this with mirage/ocaml-crunch, and we can have an ocaml library/executable
which can embeds the few python files it needs!
--> deployment heaven

@mooreryan
Copy link
Owner

mooreryan commented Jan 31, 2022

I saw that issue on the pyml repository as well. Yes that is correct that you need to use Py.Import.import_module, and in fact, that is what pyml_bindgen is doing (see here for example).

However, it doesn't change the fact that you will need to let Python know how to actually find the module. The simplest way to do that is probably with the PYTHONPATH environment variable (or just installing the package). This readme has an example of this.

(Perhaps I'm misunderstanding your question...?)

@mooreryan
Copy link
Owner

(But yes embedding the python file in the executable somehow would be very cool....)

@UnixJunkie
Copy link
Author

ok, PYTHONPATH would be the basic way, so if should let opam install the python file somewhere and then reflect
this in PYTHONPATH

@mooreryan
Copy link
Owner

Sorry for the confusion...let me try an explain it a different way in case others have the same question as well.

If you have a python package installed in the "normal" way...say pip install matplotlib or whatever, then the pyml/pyml_bindgen generated library (which uses Py.Import.import_module) will be able to find any of the matplotlib modules because it will be installed in a location in which python "looks" for modules.

If you want to use a python module not installed with pip, conda, or whatever (e.g., just in some folder in your projects directory for example) then the (probably) best way to let python know about that module is to add the directory to the PYTHONPATH.

@UnixJunkie
Copy link
Author

No problem, this is what I got from your explanations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants