PyCall @pyimport from scipy.stats import norm

using PyCall 
@pyimport from scipy.stats import norm

Giving error:

ERROR: LoadError: ArgumentError: usage @pyimport module [as name]
Stacktrace:
 [1] pyimport_name(name::Symbol, optional_varname::Tuple{Expr, Expr})
   @ PyCall C:\Users\abcdef\.julia\packages\PyCall\1gn3u\src\PyCall.jl:585
 [2] var"@pyimport"(__source__::LineNumberNode, __module__::Module, name::Any, optional_varname::Vararg{Any})
   @ PyCall C:\Users\abcdef\.julia\packages\PyCall\1gn3u\src\PyCall.jl:598
in expression starting at REPL[26]:1

How the import should be done?

This will work: norm = pyimport("scipy.stats.norm")

So will this: @pyimport scipy.stats.norm as norm

1 Like

This might be of help Modules · The Julia Language

Macros work on Julia syntax, that is too Python, hence the deviation in usage. Hypothetically, arbitrary syntax could be written in the string and parsed by a function or string literal macro, but that didn’t happen.

From the docstring: “@pyimport module as name is equivalent to const name = pywrap(pyimport("module")).” The pywrap is needed to make a Julia module with member access, which the pyimport output lacks. norm is not a Python module however, stats is.

julia> norm = pyimport("scipy.stats.norm")
ERROR: PyError (PyImport_ImportModule

The Python package scipy.stats.norm could not be imported by pyimport. Usually this means
that you did not install scipy.stats.norm in the Python version being used by PyCall.

A workaround :slight_smile:

julia> py"""
       from scipy.stats import norm
       def pynorm(x):
           return norm.cdf(x)
       """

julia> py"pynorm"(-1)
1 Like

Oh, I didn’t realize norm isn’t a package. In that case you could do @pyimport scipy.stats as stats and stats.norm

1 Like

I’d hazard a guess at the equivalent to from scipy.stats import norm being const norm = pywrap(pyimport("scipy.stats")).norm. But I wouldn’t want to lose the Julia module without a reference, I don’t think a repeat call is as smooth as another import statement in Python.

1 Like