# How to run an entire python script in pycall

**URL:** <https://discourse.julialang.org/t/how-to-run-an-entire-python-script-in-pycall/73414>\
**Category:** New to Julia\
**Tags:** pycall\
**Created:** [December 21, 2021, 3:37am UTC](https://discourse.julialang.org/t/how-to-run-an-entire-python-script-in-pycall/73414 "2021-12-21T03:37:59Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [December 21, 2021, 3:38am UTC](https://discourse.julialang.org/t/how-to-run-an-entire-python-script-in-pycall/73414/1 "2021-12-21T03:38:00Z")

</div>

Hi all

**I am using PyCall inside Pluto but I can’t run a whole python script, can anyone point me at any examples of this please?**

getting to grips with julia using Pluto and it’s fun so far. With a lot of GREAT help from the community I have PyCall able to

```julia
py_script = pyimport("test")

```

BUT every example of PyCall code seems to only allow running segments of the python script. I can’t find any examples that would run a whole script like this one called test.py.

```julia
import pandas as pd
import numpy as np
import uuid
env_dir = '/home/dave/j_sandbox/'

dev_env_csv = env_dir + '/csv/' # where to get the expert symbols from

expert_symbols_csv = "tontine_symbols.csv" # the symbols table with SPX index added AND the category stuff added 4 9 20

def get_stuff(csv_file):

     df_symbol_to_process_list = pd.read_csv( csv_file )
              
     return df_symbol_to_process_list
    
if __name__ == ' __main__' :

    mac_address = hex(uuid.getnode())
    
    df_expert_symbols = get_stuff (dev_env_csv + expert_symbols_csv)

```

I am using the latest julia 1.7.0 and the latest Pluto v0.17.2. I am also ONLY using the Pluto package manager ONLY. I am a noob so this is my first real julia program.

---

<div class="post-metadata">

**Author:** ![k945](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/k945/32/8906_2.png) [@k945](https://discourse.julialang.org/u/k945)\
**Post date:** [December 21, 2021, 4:48am UTC](https://discourse.julialang.org/t/how-to-run-an-entire-python-script-in-pycall/73414/2 "2021-12-21T04:48:05Z")

</div>

Have you tried

```julia
py"""
your code
"""

```

Have a look at the usage section in [https://github.com/JuliaPy/PyCall.jl](https://github.com/JuliaPy/PyCall.jl)

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [December 21, 2021, 6:13am UTC](https://discourse.julialang.org/t/how-to-run-an-entire-python-script-in-pycall/73414/3 "2021-12-21T06:13:53Z")

</div>

if you’re running the whole script and has no interaction between julia, you can simply run it via `run()` shell interaction

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [December 21, 2021, 11:24am UTC](https://discourse.julialang.org/t/how-to-run-an-entire-python-script-in-pycall/73414/4 "2021-12-21T11:24:06Z")

</div>

hi there  
the script “will” interact with my julia code as it would populate a dataframe. I did look into the shell approach but want to try to get this working. Thanks for the suggestion.

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [December 21, 2021, 11:27am UTC](https://discourse.julialang.org/t/how-to-run-an-entire-python-script-in-pycall/73414/5 "2021-12-21T11:27:35Z")

</div>

Hi there

I read it but couldn’t figure out how it was implemented in PyCall. I read it as a separate approach. On your suggestion I went back. Reread it and then found an example

```julia
using PyCall
py"""
def print_one_number(my_number):  
    print(my_number)   
    return my_number
"""
my_number_to_print = 10
py"print_one_number"(my_number_to_print)

```

I still don’t get how this works in running my code I can see how functions are called but not the whole script. My script just runs from main and produces a dataframe. Thanks for the suggestion though

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 21, 2021, 12:46pm UTC](https://discourse.julialang.org/t/how-to-run-an-entire-python-script-in-pycall/73414/6 "2021-12-21T12:46:02Z")

</div>

> [@anon69491625](#):
>
> BUT every example of PyCall code seems to only allow running segments of the python script. I can’t find any examples that would run a whole script like this one called test.py.

As I understand it, you want to execute an external Python script as if it were in a `py"..."` macro, _not_ as a separate Python module, so that the globals it defines are available for subsequent `py"..."` evaluations? i.e. you want the equivalent of `execfile("test.py")` in Python 2 or `exec(open("test.py").read())` in Python 3?

PyCall doesn’t export something like this right now, but it has this facility. Define:

```julia
pyinclude(fname) = (PyCall.pyeval_(read(fname, String), PyCall.pynamespace(Main), PyCall.pynamespace(Main), PyCall.Py_file_input, fname); nothing)

```

and then just do `pyinclude("test.py")`.

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [December 21, 2021, 1:09pm UTC](https://discourse.julialang.org/t/how-to-run-an-entire-python-script-in-pycall/73414/7 "2021-12-21T13:09:31Z")

</div>

Hi @stevengj thank you for the reply. You are absolutely correct, I “just” want to run an existing python script (“simple.py”) which is in the same directory as the julia script. I just want to start off on the right foot and it seemed that this was a way to reuse some of my existing code. I have no problem setting it up as a python module if that makes more sense.

As you can see ( sorry about the image I don’t know how to get at the Pluto julia code yet to paste it) I can get at the functions in the python program but not run it. I tried your define and got

```julia
UndefVarError: pynamespace not defined

```

here’s the “code” so far ( again sorry about the image)

 ![Screenshot from 2021-12-21 07-03-21](https://global.discourse-cdn.com/julialang/original/3X/1/1/1158d94985283c84c236d96dd47f9e12841031e2.png)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 21, 2021, 1:11pm UTC](https://discourse.julialang.org/t/how-to-run-an-entire-python-script-in-pycall/73414/8 "2021-12-21T13:11:25Z")

</div>

> [@anon69491625](#):
>
> `UndefVarError: pynamespace not defined`

There was a typo in my code above — it should have been `PyCall.pynamespace` (since that function is not exported). I fixed the code above; try it again.

PS. Never post screenshots of code.

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [December 21, 2021, 1:18pm UTC](https://discourse.julialang.org/t/how-to-run-an-entire-python-script-in-pycall/73414/9 "2021-12-21T13:18:03Z")

</div>

> [@anon69491625](#):
>
> ```julia
> using PyCall
> py"""
> def print_one_number(my_number):  
> print(my_number)   
> return my_number
> """
> my_number_to_print = 10
> py"print_one_number"(my_number_to_print)
> 
> ```

If you give the Python function a Julia name, you can call it directly from Julia:

```julia
using PyCall
py"""
def print_one_number(my_number):  
    print(my_number)   
    return my_number
"""
print_one_number_py = py"print_one_number" # only the 1-line version of the py macro yields the return value back to Julia!
my_number_to_print = 10
print_one_number_py(my_number_to_print)

```

---

<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:** [December 21, 2021, 1:18pm UTC](https://discourse.julialang.org/t/how-to-run-an-entire-python-script-in-pycall/73414/10 "2021-12-21T13:18:45Z")

</div>

The following should do the same:

```julia
py"""
exec(open("somefile.py").read(), globals(), locals())
"""

```

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [December 21, 2021, 1:19pm UTC](https://discourse.julialang.org/t/how-to-run-an-entire-python-script-in-pycall/73414/11 "2021-12-21T13:19:46Z")

</div>

thank you. I changed the code and there is no error but I can’t see the populated variables. For instance how do I see mac\_address which was populated by the “simple.py” script.

VERY sorry about the image, it won’t happen again.

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [December 21, 2021, 1:21pm UTC](https://discourse.julialang.org/t/how-to-run-an-entire-python-script-in-pycall/73414/12 "2021-12-21T13:21:11Z")

</div>

hi there  
that’s calling a function. I can do that but I want to run the whole script. That’s the question.  
thanks though

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 21, 2021, 1:21pm UTC](https://discourse.julialang.org/t/how-to-run-an-entire-python-script-in-pycall/73414/13 "2021-12-21T13:21:51Z")

</div>

> [@cjdoris](#):
>
> The following should do the same:

Almost. The only problem with doing `exec` in Python is that it doesn’t know the filename, so that if you have syntax errors in Python it won’t say the filename in the error message.

For that matter, you can also do:

```julia
py"""
$$(read("foo.py", String))
"""

```

I’ll add a `@pyinclude("foo.py")` macro to a future PyCall release: [add pyinclude macro by stevengj · Pull Request #948 · JuliaPy/PyCall.jl · GitHub](https://github.com/JuliaPy/PyCall.jl/pull/948)

---

<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:** [December 21, 2021, 1:24pm UTC](https://discourse.julialang.org/t/how-to-run-an-entire-python-script-in-pycall/73414/14 "2021-12-21T13:24:30Z")

</div>

Ok then

```julia
py"""
exec(compile(open("somefile.py").read(), "somefile.py", "exec"), globals(), locals())
"""

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 21, 2021, 1:24pm UTC](https://discourse.julialang.org/t/how-to-run-an-entire-python-script-in-pycall/73414/15 "2021-12-21T13:24:48Z")

</div>

> [@anon69491625](#):
>
> For instance how do I see mac\_address which was populated by the “simple.py” script.

It’s a Python variable, so you need `py"mac_address"`?

If you are in Pluto, however, evaluating in `pynamespace(Main)` may be the wrong module — Pluto code may run in some other module? My upcoming `@pyinclude` macro will fix this because it knows the current module, but in the meantime I would recommend doing:

```julia
pyinclude(fname) = py"""
exec(compile(open($fname).read(), $fname, "exec"), globals(), locals())
"""

```

instead.

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [December 21, 2021, 1:27pm UTC](https://discourse.julialang.org/t/how-to-run-an-entire-python-script-in-pycall/73414/16 "2021-12-21T13:27:39Z")

</div>

@cjdoris @stevengj

I tried both but I am certainly missing something here. When I run the cells ( either approach) I would expect to see mac\_address and df\_expert\_symbols in the julia space but I don’t. Running either approach causes no errors, no warnings just nothing.

I you look at the image ( again sorry about that) you’ll see that I can call a function but NOT the script.

what am I missing please?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 21, 2021, 1:30pm UTC](https://discourse.julialang.org/t/how-to-run-an-entire-python-script-in-pycall/73414/17 "2021-12-21T13:30:00Z")

</div>

> [@anon69491625](#):
>
> I can’t find any examples that would run a whole script like this one called test.py.

(In general, we tend to avoid code development oriented around “scripts” full of global variables. It’s a lot more flexible to work with local variables and functions, and to translate Python code into Julia line-by-line. That’s perhaps why there hasn’t been much demand for a `pyinclude`-like function over the years.)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 21, 2021, 1:31pm UTC](https://discourse.julialang.org/t/how-to-run-an-entire-python-script-in-pycall/73414/18 "2021-12-21T13:31:36Z")

</div>

> [@anon69491625](#):
>
> I would expect to see mac\_address and df\_expert\_symbols in the julia space

As I wrote above:

> [@stevengj](#):
>
> It’s a Python variable, so you need `py"mac_address"` ?

PyCall does not automatically turn Python global variables into Julia global variables; you still have to access the former through `py"..."`.

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [December 21, 2021, 1:32pm UTC](https://discourse.julialang.org/t/how-to-run-an-entire-python-script-in-pycall/73414/19 "2021-12-21T13:32:38Z")

</div>

hi @stevengj

a little more confused now, sorry this is all new to me. If I’m running a python script called simple.py what would be the correct Pluto cell to enter?

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [December 21, 2021, 1:32pm UTC](https://discourse.julialang.org/t/how-to-run-an-entire-python-script-in-pycall/73414/20 "2021-12-21T13:32:58Z")

</div>

Just wrap your script into a function on Python side?

[Next page](https://discourse.julialang.org/t/how-to-run-an-entire-python-script-in-pycall/73414.md?page=2)
