# Define Python function in Julia file with PythonCall?

**URL:** <https://discourse.julialang.org/t/define-python-function-in-julia-file-with-pythoncall/98593>\
**Category:** General Usage\
**Tags:** pycall, python, pythoncall\
**Created:** [May 10, 2023, 6:01am UTC](https://discourse.julialang.org/t/define-python-function-in-julia-file-with-pythoncall/98593 "2023-05-10T06:01:25Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [May 17, 2023, 7:42am UTC](https://discourse.julialang.org/t/define-python-function-in-julia-file-with-pythoncall/98593/7 "2023-05-17T07:42:19Z")

</div>

Whenever you use `pyexec` you’re invoking the Python parser, which makes for an unfair comparison. It’s better to use it to create a Python function then call that:

```julia
@pyexec """
def fib(n):
   a, b = 0, 1
   while a < n:
       print(a, end=' ')
       a, b = b, a+b
   print()
""" => fib

fib(10)

```

This example uses `@pyexec`’s handy `=>` notation for getting variables into and out of Python.

---

_[View the full topic](https://discourse.julialang.org/t/define-python-function-in-julia-file-with-pythoncall/98593)._
