Skip to content

Problem importing python file from current directory #502

@dompas

Description

@dompas

I have tried the solution suggested on #48 , but it is not working. I am using Julia 0.6.2 on Windows with Python 2.7 and the program is

using PyCall
unshift!(PyVector(pyimport("sys")["path"]),"")
@pyimport Leitura_Sistema_REDS as ls

and I get the error message

ERROR: LoadError: PyError (ccall(@pysym(:PyImport_ImportModule), PyPtr, (Cstring,), name)

The Python package Leitura_Sistema_REDS could not be found by pyimport. Usually this means
that you did not install Leitura_Sistema_REDS in the Python version being used by PyCall.

Activity

YP-Ye

YP-Ye commented on Oct 24, 2018

@YP-Ye

I get the same error. unshift!(PyVector(pyimport("sys")["path"]),"") does not work.

stevengj

stevengj commented on Oct 25, 2018

@stevengj
Member

What is your current directory? i.e. what is pwd()? Are you sure that you are in the directory with the module that you want?

YP-Ye

YP-Ye commented on Oct 25, 2018

@YP-Ye

Sorry, I found that my problem is caused by a python module named gurobipy. The problem is solved after reinstalling the module.

tkf

tkf commented on Oct 26, 2018

@tkf
Member

Adding an empty string to sys.path is not really a good practice. I think the right approach is pushfirst!(PyVector(pyimport("sys")["path"]), @__DIR__). This way, running Julia script works in any directory. This is close to what Python interpreter does when executing a script (it does not add "" to sys.path). Here is an example:

$ cat script.jl
using PyCall
pushfirst!(PyVector(pyimport("sys")["path"]), @__DIR__)
pyimport("mymodule")[:main]()

$ cat mymodule.py
def main():
    import sys
    print(*sys.path, sep="\n")


if __name__ == "__main__":
    main()

$ cd /

$ julia /PATH/TO/DIR/script.jl
/PATH/TO/DIR
/usr/lib/python37.zip
/usr/lib/python3.7
/usr/lib/python3.7/lib-dynload
/usr/lib/python3.7/site-packages

$ python /PATH/TO/DIR/mymodule.py  # prints the same paths

Of course, pyimport has to be done in __init__ if you are creating a Julia module.

(We probably should add this to README)

MaxandreJ

MaxandreJ commented on Nov 6, 2018

@MaxandreJ

In the README I think instead of suggesting the command pushfirst!(PyVector(pyimport("sys")["path"]), "") I would have written pushfirst!(PyVector(pyimport("sys")["path"]), "<path_to_be_added>") or something like this. I found myself replacing "path" by the path I wanted to add... I'm sure other people will make the same mistake.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @tkf@stevengj@MaxandreJ@YP-Ye@dompas

        Issue actions

          Problem importing python file from current directory · Issue #502 · JuliaPy/PyCall.jl