Run python script from c# in unity game? - python

I'm trying to create the nim game in unity with some nice 3d graphics. For those who don't know, nim is a solved game which means that, assuming both players play perfectly, we can know who will win at the first turn.
I want to make 4 game modes:
Two players who play against each other.
The player plays against the computer who plays perfectly.
The computer plays against a bot the user wrote in python.
The player plays against a bot he wrote in python.
I learnt about this game in computer science class in high school, and we were given an exercise to write a program that will win a simpler version of this game.
That's why I want to include the last two modes: To let students write their bots in python and test it in my game. I think it can turn out very cool.
However, that means I need to figure out how to run a python script from c# script in unity. I know I can use Process in c# to run the external python script, but that requires me to know the location of the python executable.
I could make a settings menu, so the user can set the path to python there, but I don't like the fact that the user will have to deal with path settings.
I learnt that, on windows, I can download an embeddable zip which contains the python interpreter (python.exe) and simply ship this with the game so the user doesn't even need to have python installed on their machine. However, I couldn't find any similar zip for other platforms, specifically linux and mac.
So, my question is how should I run python scripts in unity? Is there a way to embed the python interpreter for both windows, mac and linux, or should I make a settings menu for configuring the python path and use processes?
If somebody can give me an idea for how to use python in unity, I'd really appreciate this.
Note: I want to use python 3 for the scripting, so solutions that work for python 2 only can't help me at all.
Thanks in advance.

You can use System.Diagnostics.Process.Start and start cmd with it
string strCmdText;
strCmdText= "python script.py";
System.Diagnostics.Process.Start("CMD.exe",strCmdText);```

Only python 2.7.5, python 3 is not functional
Python and Unity
but if you just want to use a python as Bot, i suggest you to communicate via TCP (socket) or others ways...

Related

Does each IDE needs its own Python?

I have PyCharm, Visual Studio Code and Jupyter Lab installed on my PC, and the PC has Python 3.11 installed?
Would someone please educate me on this topic?
If I want run 3 Python programs simultaneously (each program uses its own IDE). Would each IDE run faster if each IDE has its own Python program? Right now all IDEs are using the same Python 3.11 and things are working fine. Just for my own knowledge, I am just curious of this topic. Thank you in advance for educating me!
You have to discriminate the two following concepts: program and process.
Program
A program is a list of instructions. It's generally written on a file.
It can be a binary text but also an ASCII text.
A program does nothing. It is basically a static text.
Process
A process is the execution of a program by your operating system.
A process has a dedicated memory and execution time. Its role is to "play" your code, instruction by instruction.
In your example, python.exe is a program.
When you execute it, your operating system will create a dedicated process.
So, when you run several IDEs, you simply run several process of your same program.
Thus, there is absolutely no loss in terms of performance.

Are there any other ways to share / run code?

So I just created a simple script with selenium that automates the login for my University's portal. The first reaction I got from a friend was: ah nice, you can put that on my pc as well. That would be rather hard as he'd have to install python and run it through an IDE or through his terminal or something like that and the user friendliness wouldn't be optimal.
Is there a way that I could like wrap it in a nicer user interface, maybe create an app or something so that I could just share that program? All they'd have to do is then fill in their login details once and the program then logs them in every time they want. I have no clue what the possibilities for that are, therefore I'm asking this question.
And more in general, how do I get to use my python code outside of my IDE? Thusfar, I've created some small projects and ran them in PyCharm and that's it. Once again, I have no clue what the possibilities are so I also don't really know what I'm asking. If anyone gets what I mean by using my code further than only in my IDE, I'd love to hear your suggestions!
The IDE running you program is the same as you running your program in the console. But if you dont want them to have python installed (and they have windows) you can maybe convert them to exe with py2exe. But if they have linux, they probably have python installed and can run you program with "python script.py". But tell your friends to install python, if they program or not, it will always come in handy

How can I run a Python 3 program if a folder is selected

Say I have a folder called "Family Photos" and I want to automatically run a python program if that folder is selected. How would I go about doing that? Would I just put the code in the folder and it runs automatically?
Edit: I'm on Windows 10
You can use tkinter in python.
Tkinter is Python's de-facto standard GUI (Graphical User Interface) package. It is a thin object-oriented layer on top of Tcl/Tk.Tkinter is not the only GuiProgramming toolkit for Python. It is however the most commonly used one
You can use other GuiProgramming toolkits as well. Follow this_link to know about other gui frameworks.
For how part in Tkinter .Follow this_link
Some unix/linux systems are open source so you could modify the OS behavior to do that, i don't believe windows offer this feature, you probably should create an APP for that
The best solution would be a python scripts that run it self each hour and check if this folder is modified and do something if its, and it will run each time you turn on the computer once

How can I embed a windows system terminal/console in a PyQt GUI Application?

im trying to have a console inside the pyqt gui i have been working on this for so long trying so many different things and trying to explain it to so many people here is what i need.
i am looking to create a cmd inside of my Pyqt4 Gui. like shown in this picture
MIND YOU this is just me moving the cmd from the program into the gui. in the picture below i have scribbled out with a red pen the boarder of the cmd(from the pythonw program) to show that i am looking to have the cmd inside not actually have the boarder. basically summed up i need a console embedded into the gui. that i can run commands threw it (NOT A PYTHON SHELL) only from the main code. my brain cant take the pain anymore and my tylenol is almost gone just from this one project. if some python master who thinks they could do this and would be willing to create this for me. i would appreciate it so much
if you want me to explain it to you in person you can join my discord server i will be here all day https://discord.gg/s63ZxTt

Running a python app alongside the live interpereter

I'm making a drawing program with python and pygame. I am trying to incorporate a script-fu thing in which the program opens a python live interpreter upon startup and allows the user to execute commands in the interpreter alongside the graphical interface.
My current strategy is to run the main loop inside its own thread, then have the application opened using a bash script that does 'python -i main.py'
Is this a safe/effective/ideal way of doing this? How can I use locks to ensure that commands coming in from the interpreter are executed between main loop iterations?
This is my first time using threads, so please explain to me like I am 7.
Thanks :)
The interpreter won't cooperate with locks you set (since it doesn't know about them). Thus, you cannot guarantee when the code entered by the user will execute.
Consider using the code module to build your own interactive console (it's really easy!). Then you can do the locking every time you go to execute user input.
Why are you using a third party live interpreter? Do you realize that pygame comes with one built in? The documentation is here. This will eliminate all of your problems quite easily.

Categories

Resources