Hello, Im trying to import a file with functions and classes written in python.
i have tried using this:
pushfirst!(PyVector(pyimport("sys")."path"), "")
x = pyimport("Models.py")
or
x = pyimport("Models")
Models.py is in the same directory
and other way i found but nothing is working.
i get the list of directories like PyObject [ '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/p ...] with also the directory im working on. so i think the problem should be here x = pyimport("Models")
pushfirst!(PyVector(pyimport("sys")."path"), "")
x = pyimport("Models")
works for me just fine. Are you sure that Models.py is really where your julia session has its working directory?
You can check if your Julia session can access your Models.py file by something like
open("Models.py") do file
print("I can open that: $(file)")
end
Sorry that I can’t really help you then. It may be worthwhile to give some additional information about your installation, like the julia and PyCall versions you are using, the Python version linked by PyCall and whether you have anything else going on that may interfere with package loading (python or julia running in virtual environments, access restrictions or the like).
To narrow it further down, now that we’re sure that Modules.py is where it’s supposed to be, can you try accessing the path you pushed to pyimport("sys")."path" from withing Python (via PyCall, not a separate instance) and see if Modules.py is visible there?
The following little example works for me. Maybe this helps you
Code of Python custom_print function in test.py:
def custom_print(arg_test):
print(arg_test)
the file is stored in the path, which I have specified under the environment variable PYTHONPATH (it should also work with the path, which you push with push!(pyimport("sys")."path", "/path/of/directory") ).
Commands in julia to call python function
using PyCall
test = pyimport("test")
@pycall test.custom_print("test2")::PyObject
thank you both @FPGro@Volker , i always thought the problem was in the pyimport but it would have not worked anyway even in python.
i tried running the example you gave me and they both worked so the i figured out the problem was in the import of the file.
i had:
import Layers //in another file.py
import keras
the Layer.py to import in the Models.py was in another directory.
but i always just read this part in the error window of julia:
PyError (PyImport_ImportModule
The Python package Models could not be imported by pyimport. Usually this means
that you did not install Models in the Python version being used by PyCall