Importing a .py file not working

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

Maybe the empty string should be a . ?

first!(PyVector(pyimport("sys")."path"), ".")

i tried also as you said.

i also tried: pushfirst!(PyVector(pyimport("sys")."path"), pwd())

im still getting: PyError (PyImport_ImportModule

Hey there,
I can’t reproduce your error.

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

which should not throw an error.

Are you sure that Models.py is really where your julia session has its working directory?

yes im sure. i just tried your example and got:

I can open that: IOStream(<file Models.py>)

i also tried using:

open(pwd()*"/Models.py") do file
    print("I can open that: $(file)")
end

which works.

Models.py is made of 2 def and 2 imports im not sure if it is a problem

FPGro

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?

For me the problem was solved by downgrading the Python version of 3.7 to 3.6 (https://techytok.com/lesson-interacting-with-python/ )

See in:

1 Like

@FPGro
Hello, does the file .py need to be in any specific state? or to have anything in particular?

my file has just 2 functions so im not sure maybe that’s the issue?

@Volker thank you but im already using python3.6

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
1 Like

Nothing that I’d know of. Completely empty files may fail to load, but something as simple as a single statement works fine for me:

julia> write("test.py", "print(\"Success!\")")
17

julia> open(readlines, "test.py")
1-element Array{String,1}:
 "print(\"Success!\")"

julia> using PyCall

julia> pushfirst!(PyVector(pyimport("sys")."path"), "")
PyObject ['', [...] ]

julia> pyimport("test");
Success!
2 Likes

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

so i thought the problem was the pyimport itself.

thank you both

1 Like