JuliaPy / PyCall.jl

Package to call Python functions from the Julia language
MIT License
1.46k stars 187 forks source link

TypeError("'module' object is not callable") #951

Closed AbhimanyuAryan closed 2 years ago

AbhimanyuAryan commented 2 years ago

I am new to PyCall. I'm trying to use playwright python API with PyCall

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page()
    page.goto("http://playwright.dev")
    print(page.title())
    browser.close()

so I basically tried

sync_playwright = pyimport("playwright.sync_api")
julia> sync_playwright
PyObject <module 'playwright.sync_api' from '/Users/arstar/miniconda3/envs/conda_jl/lib/python3.9/site-packages/playwright/sync_api/__init__.py'>

Now I want to achieve this:

sync_playwright()

but I get this error:

ERROR: PyError ($(Expr(:escape, :(ccall(#= /Users/arstar/.julia/packages/PyCall/L0fLP/src/pyfncall.jl:43 =# @pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, pyargsptr, kw))))) <class 'TypeError'>
TypeError("'module' object is not callable")

Stacktrace:
  [1] pyerr_check
    @ ~/.julia/packages/PyCall/L0fLP/src/exception.jl:62 [inlined]
  [2] pyerr_check
    @ ~/.julia/packages/PyCall/L0fLP/src/exception.jl:66 [inlined]
  [3] _handle_error(msg::String)
    @ PyCall ~/.julia/packages/PyCall/L0fLP/src/exception.jl:83
  [4] macro expansion
    @ ~/.julia/packages/PyCall/L0fLP/src/exception.jl:97 [inlined]
  [5] #107
    @ ~/.julia/packages/PyCall/L0fLP/src/pyfncall.jl:43 [inlined]
  [6] disable_sigint
    @ ./c.jl:458 [inlined]
  [7] __pycall!
    @ ~/.julia/packages/PyCall/L0fLP/src/pyfncall.jl:42 [inlined]
  [8] _pycall!(ret::PyObject, o::PyObject, args::Tuple{}, nargs::Int64, kw::Ptr{Nothing})
    @ PyCall ~/.julia/packages/PyCall/L0fLP/src/pyfncall.jl:29
  [9] _pycall!
    @ ~/.julia/packages/PyCall/L0fLP/src/pyfncall.jl:11 [inlined]
 [10] #_#114
    @ ~/.julia/packages/PyCall/L0fLP/src/pyfncall.jl:86 [inlined]
 [11] (::PyObject)()
    @ PyCall ~/.julia/packages/PyCall/L0fLP/src/pyfncall.jl:86
 [12] top-level scope
    @ REPL[4]:1
AbhimanyuAryan commented 2 years ago

ok so I managed to call

sync_playwright.sync_playwright()
PyObject <playwright.sync_api._context_manager.PlaywrightContextManager object at 0x7fa2950dde20>

but now I want to run

sync_playwright.sync_playwright().chromium.launch()

which says

sync_playwright.sync_playwright().chromium.launch()
ERROR: KeyError: key :chromium not found
Stacktrace:
 [1] __getproperty(o::PyObject, s::Symbol)
   @ PyCall ~/.julia/packages/PyCall/L0fLP/src/PyCall.jl:307
 [2] getproperty(o::PyObject, s::Symbol)
   @ PyCall ~/.julia/packages/PyCall/L0fLP/src/PyCall.jl:312
 [3] top-level scope
   @ REPL[14]:1

do I need to read python code and manually call each and every method/object/module while wrapping it with PyCall.jl?

stevengj commented 2 years ago

sync_playwright = pyimport("playwright.sync_api")

This imports the sync_api module, not the function, which is why it gives a module object. It's equivalent to import playwright.sync_api as sync_playwright, which is not what you want. You want

sync_playwright = pyimport("playwright.sync_api").sync_playwright

I guess.

The analogue of the Python with construct is @pywith. Have you tried:

@pywith sync_playwright() as p begin
    browser = p.chromium.launch()
    page = browser.new_page()
    page.goto("http://playwright.dev")
    println(page.title())
    browser.close()
end

?

If you are still getting an issue with p.chromium, maybe you have some missing component in your Python installation.

AbhimanyuAryan commented 2 years ago

@stevengj so basically. playwright after conda installation asks you to install chromium firefox components which you do by calling

playwright install

from terminal (in conda_jl environment). It worked downloaded chromium, firefox etc. Works in Python version. But this doesn't work in PyCall. I didn't call playwright install from conda.jl or pycall.jl I was not sure how. Instead called it from python's virtual env conda_jl which is configured with conda.jl and python.jl

Was I suppose to call playwright install via conda.jl or pycall.jl

I installed playwright with Conda.pip("install", "playwright")

AbhimanyuAryan commented 2 years ago

ok it worked 2nd time. Thanks a ton @stevengj

julia> @pywith sync_playwright() as p begin
           browser = p.chromium.launch()
           page = browser.new_page()
           page.goto("http://playwright.dev")
           println(page.title())
           browser.close()
       end
Fast and reliable end-to-end testing for modern web apps | Playwright

First time it gave me error:

julia> @pywith sync_playwright() as p begin
           browser = p.chromium.launch()
           page = browser.new_page()
           page.goto("http://playwright.dev")
           println(page.title())
           browser.close()
       end
ERROR: PyError ($(Expr(:escape, :(ccall(#= /Users/arstar/.julia/packages/PyCall/L0fLP/src/pyfncall.jl:43 =# @pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, pyargsptr, kw))))) <class 'playwright._impl._api_types.Error'>
Error('net::ERR_NAME_NOT_RESOLVED at http://playwright.dev/\n=========================== logs ===========================\nnavigating to "http://playwright.dev/", waiting until "load"\n============================================================')
  File "/Users/arstar/miniconda3/envs/conda_jl/lib/python3.9/site-packages/playwright/sync_api/_generated.py", line 7278, in goto
    self._sync(
  File "/Users/arstar/miniconda3/envs/conda_jl/lib/python3.9/site-packages/playwright/_impl/_sync_base.py", line 111, in _sync
    return task.result()
  File "/Users/arstar/miniconda3/envs/conda_jl/lib/python3.9/site-packages/playwright/_impl/_page.py", line 466, in goto
    return await self._main_frame.goto(**locals_to_params(locals()))
  File "/Users/arstar/miniconda3/envs/conda_jl/lib/python3.9/site-packages/playwright/_impl/_frame.py", line 119, in goto
    await self._channel.send("goto", locals_to_params(locals()))
  File "/Users/arstar/miniconda3/envs/conda_jl/lib/python3.9/site-packages/playwright/_impl/_connection.py", line 39, in send
    return await self.inner_send(method, params, False)
  File "/Users/arstar/miniconda3/envs/conda_jl/lib/python3.9/site-packages/playwright/_impl/_connection.py", line 63, in inner_send
    result = next(iter(done)).result()

Stacktrace:
 [1] top-level scope
   @ ~/.julia/packages/PyCall/L0fLP/src/PyCall.jl:656

caused by: PyError ($(Expr(:escape, :(ccall(#= /Users/arstar/.julia/packages/PyCall/L0fLP/src/pyfncall.jl:43 =# @pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, pyargsptr, kw))))) <class 'playwright._impl._api_types.Error'>
Error('net::ERR_NAME_NOT_RESOLVED at http://playwright.dev/\n=========================== logs ===========================\nnavigating to "http://playwright.dev/", waiting until "load"\n============================================================')
Loop <_UnixSelectorEventLoop running=False closed=True debug=False> that handles pid 51837 is closed
  File "/Users/arstar/miniconda3/envs/conda_jl/lib/python3.9/site-packages/playwright/sync_api/_generated.py", line 7278, in goto
    self._sync(
  File "/Users/arstar/miniconda3/envs/conda_jl/lib/python3.9/site-packages/playwright/_impl/_sync_base.py", line 111, in _sync
    return task.result()
  File "/Users/arstar/miniconda3/envs/conda_jl/lib/python3.9/site-packages/playwright/_impl/_page.py", line 466, in goto
    return await self._main_frame.goto(**locals_to_params(locals()))
  File "/Users/arstar/miniconda3/envs/conda_jl/lib/python3.9/site-packages/playwright/_impl/_frame.py", line 119, in goto
    await self._channel.send("goto", locals_to_params(locals()))
  File "/Users/arstar/miniconda3/envs/conda_jl/lib/python3.9/site-packages/playwright/_impl/_connection.py", line 39, in send
    return await self.inner_send(method, params, False)
  File "/Users/arstar/miniconda3/envs/conda_jl/lib/python3.9/site-packages/playwright/_impl/_connection.py", line 63, in inner_send
    result = next(iter(done)).result()

Stacktrace:
  [1] pyerr_check
    @ ~/.julia/packages/PyCall/L0fLP/src/exception.jl:62 [inlined]
  [2] pyerr_check
    @ ~/.julia/packages/PyCall/L0fLP/src/exception.jl:66 [inlined]
  [3] _handle_error(msg::String)
    @ PyCall ~/.julia/packages/PyCall/L0fLP/src/exception.jl:83
  [4] macro expansion
    @ ~/.julia/packages/PyCall/L0fLP/src/exception.jl:97 [inlined]
  [5] #107
    @ ~/.julia/packages/PyCall/L0fLP/src/pyfncall.jl:43 [inlined]
  [6] disable_sigint
    @ ./c.jl:458 [inlined]
  [7] __pycall!
    @ ~/.julia/packages/PyCall/L0fLP/src/pyfncall.jl:42 [inlined]
  [8] _pycall!(ret::PyObject, o::PyObject, args::Tuple{String}, nargs::Int64, kw::Ptr{Nothing})
    @ PyCall ~/.julia/packages/PyCall/L0fLP/src/pyfncall.jl:29
  [9] _pycall!
    @ ~/.julia/packages/PyCall/L0fLP/src/pyfncall.jl:11 [inlined]
 [10] #_#114
    @ ~/.julia/packages/PyCall/L0fLP/src/pyfncall.jl:86 [inlined]
 [11] (::PyObject)(args::String)
    @ PyCall ~/.julia/packages/PyCall/L0fLP/src/pyfncall.jl:86
 [12] macro expansion
    @ REPL[25]:4 [inlined]
 [13] top-level scope
    @ ~/.julia/packages/PyCall/L0fLP/src/PyCall.jl:652
AbhimanyuAryan commented 2 years ago

looks like network error: net::ERR_NAME_NOT_RESOLVED