A issue to use PythonCall to call a Simple Python File Of WxPython Hello World

Hello, I am new to Julia and just start to learn it this week. I got a problem to use PythonCall to call a Simple Python File Of WxPython Hello World. My Julia code and Python code are below. When I ran Julia code the below an error came out, I have searched online and tried many ways, but no luck. It looks like it can not find python.exe, but I am sure it is under the folder “C:\Python-3.12\python.exe”. is there a way to fix it? Thanks a lot! I appreciate it.

Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Apps > Advanced app settings > App execution aliases.
ERROR: failed process: Process(`python 'C:\JuliaTryOut\Test01.py'`, ProcessExited(9009)) [9009]

Stacktrace:
 [1] pipeline_error
   @ .\process.jl:597 [inlined]
 [2] run(::Cmd; wait::Bool)
   @ Base .\process.jl:512
 [3] run(::Cmd)
   @ Base .\process.jl:509
 [4] top-level scope
   @ c:\JuliaTryOut\Test01.jl:18

-------------------My Julia Code---------------------------------------------------------

 ENV["JULIA_CONDAPKG_BACKEND"] = "Null";

 ENV["JULIA_PYTHONCALL_EXE"] = "C:\\Python-3.12\\python.exe"

 using PythonCall

 pyimport("sys").path.append(pwd())

 println(PythonCall.C.CTX.exe_path)

 #pyexec(read("Test01.py", String));

 run(`python C:\\JuliaTryOut\\Test01.py`)

-------------------My Python File Test01.py-------------------------------------------------------------

import wx

class MyFrame(wx.Frame):
    def __init__(self):
        # Initialize the parent class (wx.Frame)
        super().__init__(parent=None, title='Hello World', size=(300, 200))

        # Add a panel to the frame for better layout management
        panel = wx.Panel(self)

        # Add static text to the panel
        self.text = wx.StaticText(panel, label="Hello, wxPython!", pos=(100, 70))


if __name__ == '__main__':
    # 1. Initialize the Application
    app = wx.App()

    # 2. Create and show the main frame
    frame = MyFrame()
    frame.Show()

    # 3. Start the main event loop
    app.MainLoop()

Hello,

Welcome to the community!

Can you please enclose the code and the error message in your message in triple backticks, like ``` ?

That makes it easier to read.

ufechner7, is the below format one you want?

-------------------Error Message---------------------------------------------------------
‘’’
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Apps > Advanced app settings > App execution aliases.
ERROR: failed process: Process(python 'C:\JuliaTryOut\Test01.py', ProcessExited(9009)) [9009]

Stacktrace:
[1] pipeline_error
@ .\process.jl:597 [inlined]
[2] run(::Cmd; wait::Bool)
@ Base .\process.jl:512
[3] run(::Cmd)
@ Base .\process.jl:509
[4] top-level scope
@ c:\JuliaTryOut\Test01.jl:18
‘’’

-------------------My Julia Code---------------------------------------------------------
‘’’
ENV[“JULIA_CONDAPKG_BACKEND”] = “Null”;

ENV[“JULIA_PYTHONCALL_EXE”] = “C:\Python-3.12\python.exe”

using PythonCall

pyimport(“sys”).path.append(pwd())

println(PythonCall.C.CTX.exe_path)

#pyexec(read(“Test01.py”, String));

run(python C:\\JuliaTryOut\\Test01.py)
‘’’

-------------------My Python File Test01.py-------------------------------------------------------------
‘’’
import wx

class MyFrame(wx.Frame):
def init(self):
# Initialize the parent class (wx.Frame)
super().init(parent=None, title=‘Hello World’, size=(300, 200))

    # Add a panel to the frame for better layout management
    panel = wx.Panel(self)

    # Add static text to the panel
    self.text = wx.StaticText(panel, label="Hello, wxPython!", pos=(100, 70))

if name == ‘main’:
# 1. Initialize the Application
app = wx.App()

# 2. Create and show the main frame
frame = MyFrame()
frame.Show()

# 3. Start the main event loop
app.MainLoop()

‘’’

Sorry, you are not there yet.

Firstly, you can edit your post. Better than creating a new post.

Secondly, you used three right single quotation marks (U+2019, often called curly or typographic apostrophes). Backticks (`), by contrast, are straight grave accents (U+0060) used in computing for code blocks, template literals, and command substitution.

Try to copy-and-paste the backticks from my post if you cannot find them on your keyboard: ```

See also: Please read: make it easier to help you

2 Likes

It sounds like Python is not on your PATH. You could simply add it:

ENV["PATH"] = "C:\\Python-3.12;" * ENV["PATH"]

(note the ;).

Now your

run(`python C:\\JuliaTryOut\\Test01.py`)

should work. You can also avoid messing with PATH entirely by just providing the complete Python path:

run(`C:\\Python-3.12\\python.exe C:\\JuliaTryOut\\Test01.py`)

Note that we have not used PythonCall.jl here. If you just want to run a Python script, but not really interact with it, you really don’t need this package. But you could (probably) use

pyexec(read("C:\\JuliaTryOut\\Test01.py", String), Main) 

or

pyexec(read("C:\\JuliaTryOut\\Test01.py", String), PyDict())

If pwd() == “C:\\JuliaTryOut\\", as is presumably the case, you can just use "Test01.py". Compared to what you had commented out, you then just needed the second argument.

1 Like

When using Windows, path delimiters should either be escaped "C:\\Python-3.12\\python.exe" or you could use raw strings: raw"C:\python-3.12\python.exe".

Python was not found; run without arguments to install from the Microsoft Store

This error says you didn’t install Python properly. — your python.exe exists but is an alias that doesn’t do anything except install Python if you run it without arguments.

Have you tried running Python from the command line, without using Julia?

When using Windows, path delimiters should either be escaped

The OP was already doing this, but because they weren’t quoting their code, the escapes weren’t showing up. I edited the post to quote the code correctly so that you can now read the escapes.

Actually the OP used three apostrophes ' (U+0027) — it’s just that discourse automatically displays those as curly quotes. But your point stands that what is needed is three backticks `.

This post talks specifically about quoting: PSA: how to quote code with backticks

1 Like

eldee and others,
Thanks a lot for the clues. I followed eldee’s advice to add ENV[“PATH”] = “C:\Python-3.12;” * ENV[“PATH”]. That fixed the problem. I appreciate everyone’s assistance.

1 Like

The formatting advice here is part of Discourse’s Markdown parsing. Markdown isn’t exactly standardized in the sense that different places implement different extensions, but the basics should get most of the job done, and the interactive icons on Discourse post editors help.

Formatting posts using markdown, BBCode, and HTML - Documentation / Using Discourse - Discourse Meta

Taking a few minutes to learn the basics is worth the clarity to readers, and Markdown is lightweight enough to be readable where there is no Markdown rendering.

Although the issue was resolved by changing ENV["PATH"], these lines are a documented alternative to having the python executable on the path. You can track down the python executable path in the global Python install or a virtual environment. I personally recommend defaulting to PythonCall using CondaPkg for a Julia-first project, but these alternatives are good for adding Julia to existing Python projects.