I made a module with an if condition on the number of cores.
module mymodule
import Pkg
import PyCall
using Distributed
if nworkers() > 1
@everywhere using Pkg
@everywhere Pkg.activate(".")
@everywhere Pkg.instantiate()
@everywhere using PyCall
@everywhere @pyimport scipy.signal as ss
function parallel()
....
end
else
using Pkg
Pkg.activate(".")
Pkg.instantiate()
using PyCall
@pyimport scipy.signal as ss
function serial()
....
end
end
end #mymodule
Code throws the following error on execution
ERROR: LoadError: LoadError: UndefVarError: @pyimport not defined
Stacktrace:
[1] top-level scope
[2] include at ./boot.jl:326 [inlined]
[3] include_relative(::Module, ::String) at ./loading.jl:1038
[4] include(::Module, ::String) at ./sysimg.jl:29
[5] include(::String) at ./client.jl:403
[6] top-level scope at none:0
in expression starting at /storage/work/s/mymodule.jl:81
in expression starting at /storage/work/s/mymodule.jl:30
where line 81 is the line in else condition corresponding to @pyimport scipy.signal as ss and line 30 corresponds to if nworkers() > 1.
Before this problem, the code had an issue with the line @everywhere @pyimport scipy.signal as ss but that disappeared by using import PyCall; bizarrely though, it didn't solve the former problem.
Has anyone experienced a similar issue or aware of such issues?
You need to use pyimport function instead. Macro definition (comes from your using) and usage of that macro in the same block does not work due to the parsing/evaluation order.
Simply change the code
@pyimport scipy.signal as ss
to
ss = pyimport("scipy.signal")
You can also divide the blocks into two, first for the definitions and the second for usage. However, I would not do that as @pyimport macro is already deprecated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With