-
Notifications
You must be signed in to change notification settings - Fork 189
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
Importing local module fails #48
Comments
More technically, we are passing 0 for the updatepath parameter of I'm not sure of the best option here. One option would be to just prepend @loladiro, how does Julia decide whether to import modules from the current directory? |
Maybe a custom option to |
@stevengj Not sure what you mean. If you're talking about include it depends the most recently included file in the current task (the directory of that, or, if none |
@loladiro, if you type Furthermore, it looks like Julia has a similar security issue to Python... it looks like the current directory is searched before the other standard package directories. |
@stevengj I think it just does |
@loladiro, see my amended comment above... it looks like we may have the same security issue as in Python after all. e.g. if I add a Or maybe because it is task-dependent this is okay? |
It think this is different because |
@mlubin, regarding adding an option to
although one might provide a convenience function for this. (e.g. I could just define However, I'd like to do something sensible by default. If Python users are used to being able to load things in the current directory in |
I could mimic Julia's behavior: every |
What if we called Python's os.chdir whenever Julia's cd was called (as well as in pyinitialize)? |
How would we modify Julia's cd? |
I think I'm not understanding the security issue with always prepending sys.path with "" in pyinitialize. If I execute a file test.jl with python like
and a file called numpy.py is in the same directory as test.py, then that local numpy.py will be imported. PyCall would then have the same behavior. Is that what you're trying to avoid? |
It's not where |
It seems the path that julia is run in is independent of the search path of python modules (or I'm still not quite getting the issue): ~/a/test.jl:
~/numpy.py:
Then in the shell,
loads the numpy package, not my rogue version. |
What do you think, @stevengj? |
You're right, I can't get it to override the numpy module, but maybe I'm missing something in how Python decides what modules to load. See also the Python bug discussion. |
Github should have a system to just say: "+1" for a request. PyCall seems like a brilliant stroke of genius by the way. Thanks! |
👍 for this. If any one needs a workaround the following can be done @pyimport importlib.machinery as machinery
loader = machinery.SourceFileLoader("module.name","/abs/path/to/file.py")
my_mod = loader[:load_module]("module.name")
my_mod[:myfunc]() This loads python files by path. I got the idea from: http://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path |
@dhoegh that solution doesn't work for me. I get the following error: julia>@pyimport importlib.machinery as machinery in pyerr_check at /home/shrif/.julia/v0.3/PyCall/src/exception.jl:61 Any idea if there a machinery equivalent I can use? |
What python version are you using? It depends on the python version. I have a version that works for python 2.7 at: https://github.com/dhoegh/Hawk.jl/blob/master/src/Hawk.jl#L13 alternative see: http://stackoverflow.com/questions/19009932/import-abitrary-python-source-file-python-3-3. |
I am on 2.7 . After checking now, it seems that python2.7 also has problems importing machinery. Not quite sure why since it's included in site-packages under importlib . |
Did you try the import I used in https://github.com/dhoegh/Hawk.jl/blob/master/src/Hawk.jl#L7-13 |
Yes -- it works just fine, thanks! However, now I have a new problem. When the local module imports other local modules, the import statement fails. import-test.jl :
callmefirst.py:
callme.py:
running julia import-test.jl returns
|
Update: This is fixed when I update callmefirst.py to the following:
However, this is pretty inconvenient since I'd need to change all my import statements (especially if I'm importing a python framework which relies on calling local modules as such). Any advice on how to get around this? |
@sherifnada, does it work to just the current directory to the path, via |
Worked like a charm. Thanks!! |
@dhoegh Thanks for your workaround. This feature would be great for PyCall. |
Is there any easy way to do the same thing as |
@rofinn, that's a separate issue. You can certainly |
py"""
import sys
sys.path.insert(0, "./directory/path/containing/module")
"""
function_name = pyimport("module_name")["function_name"] |
pyimport
doesn't seem to be able to find python modules that are present in the local directory. Any reason for this? What's the best way to import user modules?The text was updated successfully, but these errors were encountered: