# Importing a .py file not working

**URL:** <https://discourse.julialang.org/t/importing-a-py-file-not-working/51366>\
**Category:** General Usage\
**Tags:** pycall\
**Created:** [December 6, 2020, 8:35pm UTC](https://discourse.julialang.org/t/importing-a-py-file-not-working/51366 "2020-12-06T20:35:23Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![lorrp1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lorrp1/32/17647_2.png) [@lorrp1](https://discourse.julialang.org/u/lorrp1)\
**Post date:** [December 6, 2020, 8:35pm UTC](https://discourse.julialang.org/t/importing-a-py-file-not-working/51366/1 "2020-12-06T20:35:23Z")

</div>

Hello, Im trying to import a file with functions and classes written in python.

i have tried using this:

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

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [December 6, 2020, 8:42pm UTC](https://discourse.julialang.org/t/importing-a-py-file-not-working/51366/2 "2020-12-06T20:42:12Z")

</div>

Maybe the empty string should be a . ?

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

```

---

<div class="post-metadata">

**Author:** ![lorrp1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lorrp1/32/17647_2.png) [@lorrp1](https://discourse.julialang.org/u/lorrp1)\
**Post date:** [December 6, 2020, 8:51pm UTC](https://discourse.julialang.org/t/importing-a-py-file-not-working/51366/3 "2020-12-06T20:51:39Z")

</div>

i tried also as you said.

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

im still getting: **PyError (PyImport\_ImportModule**

---

<div class="post-metadata">

**Author:** ![FPGro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fpgro/32/20822_2.png) [@FPGro](https://discourse.julialang.org/u/FPGro)\
**Post date:** [December 7, 2020, 8:54am UTC](https://discourse.julialang.org/t/importing-a-py-file-not-working/51366/4 "2020-12-07T08:54:48Z")

</div>

Hey there,  
I can’t reproduce your error.

```julia
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

```julia
open("Models.py") do file
    print("I can open that: $(file)")
end

```

which should not throw an error.

---

<div class="post-metadata">

**Author:** ![lorrp1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lorrp1/32/17647_2.png) [@lorrp1](https://discourse.julialang.org/u/lorrp1)\
**Post date:** [December 7, 2020, 8:09pm UTC](https://discourse.julialang.org/t/importing-a-py-file-not-working/51366/5 "2020-12-07T20:09:34Z")

</div>

> 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:

```julia
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](https://discourse.julialang.org/u/FPGro)

---

<div class="post-metadata">

**Author:** ![FPGro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fpgro/32/20822_2.png) [@FPGro](https://discourse.julialang.org/u/FPGro)\
**Post date:** [December 7, 2020, 11:57pm UTC](https://discourse.julialang.org/t/importing-a-py-file-not-working/51366/6 "2020-12-07T23:57:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Volker](https://avatars.discourse-cdn.com/v4/letter/v/77aa72/32.png) [@Volker](https://discourse.julialang.org/u/Volker)\
**Post date:** [December 8, 2020, 6:30am UTC](https://discourse.julialang.org/t/importing-a-py-file-not-working/51366/7 "2020-12-08T06:30:58Z")

</div>

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

See in:

> [@Importing a Python Module from another directory](https://discourse.julialang.org/t/importing-a-python-module-from-another-directory/28310/3):
>
> You could push the directory to Python’s sys.path and then import the module: using PyCall push!(pyimport("sys")."path", "/path/of/directory") mymodule = pyimport("mymodule")

---

<div class="post-metadata">

**Author:** ![lorrp1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lorrp1/32/17647_2.png) [@lorrp1](https://discourse.julialang.org/u/lorrp1)\
**Post date:** [December 8, 2020, 7:39pm UTC](https://discourse.julialang.org/t/importing-a-py-file-not-working/51366/8 "2020-12-08T19:39:21Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![Volker](https://avatars.discourse-cdn.com/v4/letter/v/77aa72/32.png) [@Volker](https://discourse.julialang.org/u/Volker)\
**Post date:** [December 9, 2020, 8:18am UTC](https://discourse.julialang.org/t/importing-a-py-file-not-working/51366/9 "2020-12-09T08:18:10Z")

</div>

The following little example works for me. Maybe this helps you

Code of Python custom\_print function in test.py:

```julia
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

```julia
using PyCall
test = pyimport("test")
@pycall test.custom_print("test2")::PyObject

```

---

<div class="post-metadata">

**Author:** ![FPGro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fpgro/32/20822_2.png) [@FPGro](https://discourse.julialang.org/u/FPGro)\
**Post date:** [December 9, 2020, 10:02am UTC](https://discourse.julialang.org/t/importing-a-py-file-not-working/51366/10 "2020-12-09T10:02:29Z")

</div>

> [@lorrp1](#):
>
> Hello, does the file .py need to be in any specific state? or to have anything in particular?

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
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!

```

---

<div class="post-metadata">

**Author:** ![lorrp1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lorrp1/32/17647_2.png) [@lorrp1](https://discourse.julialang.org/u/lorrp1)\
**Post date:** [December 10, 2020, 9:38pm UTC](https://discourse.julialang.org/t/importing-a-py-file-not-working/51366/11 "2020-12-10T21:38:24Z")

</div>

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:

```julia
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:

```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
