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

Problem importing python file from current directory #502

Open
dompas opened this issue May 15, 2018 · 5 comments
Open

Problem importing python file from current directory #502

dompas opened this issue May 15, 2018 · 5 comments

Comments

@dompas
Copy link

dompas commented May 15, 2018

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.
@YP-Ye
Copy link

YP-Ye commented Oct 24, 2018

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

@stevengj
Copy link
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
Copy link

YP-Ye commented Oct 25, 2018

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

@tkf
Copy link
Member

tkf commented Oct 26, 2018

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
Copy link

MaxandreJ commented Nov 6, 2018

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants