julia counterpart for "from module import some_func" in python - julia

I find using Module in julia is equivalent to from Module import * in python. Is there any way to import a single function or variable from a module.

You can import e.g. a single function with
using Module.func
If you also want to be able to extend the function with new methods, use instead
import Module.func
If you want to import several named functions and variables from the same module, you can e.g. use one of
using Module: func1, func2
import Module: func1, func2
All of these forms allow to import definitions from Module regardless of whether they are exported there, unlike using Module, which only imports exported definitions.
Also keep in mind that it is really values that are imported, not variables, so if you try to assign to an imported variable, you will not see a change in the originating module (and you will get a warning).

If you have a module Foo with a function bar (or something else specific you wan to import) you can do:
import Foo: bar
This brings only bar into scope.

Related

Why use `exec_statement` when writing PyInstaller hooks?

In the PyInstaller docs they demonstrate the use of eval_statement() and exec_statement() which call eval() or exec() in a new instance of Python. But they don't say why you would want to run your code in a separate instance.
For example, why couldn't their example of:
from PyInstaller.utils.hooks import exec_statement
mpl_data_dir = exec_statement(
"import matplotlib; print(matplotlib._get_data_path())"
)
datas = [ (mpl_data_dir, "") ]
not just be:
import matplotlib
datas = [(matplotlib._get_data_path(), "")]
I've tried doing this with my own library and it doesn't seem to do it any harm. So why the extra complexity? Why do all the other hooks included in PyInstaller use the 1st method?
All the hooks use the first method in case of path and environment manipulations. What happens if matplotlib changes sys.path or some other environmental manipulation that could mess with the build process? Using a separate python instance means this won't happen.

How to use package in Julia without importing it?

If you write it imports all bunch of methods into the current scope.
using Plots
plot(rand(5,5))
I need only one method, is it possible to write the same code but without using or importing anything, I don't want to pollute the current scope with Plots exports and don't mind to prefix it with package name every time I use it.
Plots.plot(rand(5,5))
import will bring a module into scope without any of its exported names. You can still use qualified names to reference names within the imported module:
import Plots
Plots.plot(rand(5,5))
To avoid using the qualified name, you can create a binding to a new name:
const plot_from_plots = Plots.plot
According to the Julia website you should be able to do:
using Plots: plot
Which will only bring plot() in scope
See Module aliasing in Julia for how to create an alias for the method
In addition to the other suggestions; I wrote a package (ImportMacros) which lets you import functions with other names without bringing anything else into scope:
julia> using ImportMacros
julia> #import Plots.plot as plt
julia> plt
plot (generic function with 3 methods)
so Plots.plot is available as plt, yet Plots is undefined:
julia> Plots
ERROR: UndefVarError: Plots not defined

How to use a Python module's functions inside a function you're defining WITHOUT importing beyond your function's scope (Ie: R has package::function)

Is there a way to incorporate Python functions from an external module/library in a function you're writing/defining, without importing them globally or beyond of the function scope?
For example, in R you can do package::function:
myfxn <- function(x){ # R example
dplyr::as_tibble(x)
}
I'd like to do something similar in Python, but as far as I can tell, people usually just import the modules/elements of modules at the top of the file (for speed, succinctness, comprehensiveness, etc.) I know you can technically do something like this:
Example of what I know you can do in Python (but isn't what I'm looking for):
def myfxn(x): # Python
from pandas import DataFrame
return(DataFrame(x))
...But what I'd really like is a way to use a function like the pandas DataFrame one without having to globally import it. I wish there was something like...
def myfxn(x): # Python
return(pandas.DataFrame(x))
Where I wouldn't have to actually import anything. Is there any way of doing what I'm referencing? If not, is there an argument (except for a tiny amount of processing speed) about why there shouldn't be?

Test if include/import has been already done

I have the following include/import in my "main.jl" file:
include("Global.jl")
import .Global
As importing this module takes some time and it is always the same on every execution, I would like test in advance if .Global exists so I can bypass the include/import.
The idea is that I can edit all changes in my text editor and use the interactive console to reload the whole program but waiving that step if it is already there.
import X is already basically a no-op if X has already been imported.
The problem is when you do the include you are defining a new module also called X so import tries and loads the new one.
I suggest thus converting your module into a package,
adding it to your environment, then just doing import X
You can use isdefined(Main, :ModuleName) (don't forget the colon).
if !isdefined(Main, :Global)
include("Global.jl")
import .Global
end
For the same question when you are using a package named "MyPackage":
if !isdefined(Main, :MyPackage)
using MyPackage
end

Sikuli - NameError: global name 'openApp' is not defined

I'm calling a sikuli function, inside Sikuli IDE, but I get this error "NameError: global name 'openApp' is not defined"...
If I try to do openApp('calc') in a new Sikuli blank file, it works, but if I use in another .sikuli file like:
def sample():
import myLib
# my Lib is .py file that I've created and put it on sikuli-script.jar
var = somevalue
myLib.myFunction(something)
openApp('calc')
I get the error with "openApp" and other sikuli funcions like "Key" (ex: Key.ENTER) too...
Hope I had explained that well
By default, Sikuli will insert a from sikuli import * into all main files. This error tends to occur when you import sikuli modules. If you are importing modules, you will need to add the import manually. See the documentation for more advice.
If your tests are in the same folder basically you can do,
import testName
reload(testName)
from testName import *
This will import your test and execute it's content.
testName should be name of the file without .sikuli extension
I ran into a similar problem was solved by putting from sikuli import * at the first line of any file you are importing. I hope this helps!
I only mentioned this, because with the imported files, I had the greatest overall success with this one, and it became habit to make this the first line.

Resources