Python3 interactive mode on Linux launches the codes twice - linux

I have written a chess program in Python 3.4.3 and I am running the Python 3 interpreter in the interactive mode as follows:
python3 -i chess.py
However, the code after the class definitions get invoked twice and I do not know why. My code is on pastebin

You should remove the line from chess import * that is at the end of the file, it should not be needed.
Also, it is common to make sure that some of the code is not executed unless the code in the module is executed as a script.
if __name__ == '__main__':
# Not executed if the module is imported
g = Game()

Related

How determine Python script run from IDE or standalone

I've recently started learning python and am still a newbie.
How can I determine if my code run from IDE or run standalone?
My code is not imported so
__name__ == "__main__" .
Someone suggested checking sys.executable name but I'm looking for a solution independent of the file name.
P.S: I create a standalone file with pyinstaller.
Here's a link to the page pyinstaller has on their site giving information about how to check the Run-Time information of your file: https://pyinstaller.org/en/stable/runtime-information.html
Basically it says to add the following code
import sys
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
print('running in a PyInstaller bundle')
else:
print('running in a normal Python process')
This should print the 'running in a PyInstaller bundle' message if it's all self contained and properly bundled, and it should print 'running in a normal Python process' if it's still running from your source code.

Executable file made using Pyinstaller doesn't start

I want to convert the following sample python program into an executable file:
import os
print(os.getcwd())
To convert it into an executable I have used Pyinstaller:
pyinstaller app.py --onefile
And the EXE file is getting generated in the dist folder, but when I run it, it launches and immediately closes, and the expected print statement is not even displayed.
What could be the issue?
It does prints the statement, but just after printing it the code ends. You can add input() or use time.sleep(seconds) to make your program wait until you press a key or any particular number of seconds respectivly.
To check if your code(without the advice i have given) prints, start that python file in your command line.

Run python script with module/ modules already imported

I have tried nearly all solutions in my mind to do this, but the fact is I can't run a python script with modules imported already.
Here's my code for the module cls.py:
import os
def cls():
os.system('cls')
Given below is the code for opening python in cmd:
#echo off
python
pause
What I need is to open the python in the command prompt with the module cls imported. Also, when I try python -m <module> way, it doesn't show any errors, but the program ends.
Any help would be greatly appreciated and thanks in advance.
Saw this question related to mine, but it is not my problem: Run python script that imports modules with batch file
I think what you'r looking for is the interactive mode:
-i
When a script is passed as first argument or the -c option is used, enter interactive mode after executing the script or the command
So just use python -i cls.py in your batch file. This would import your Python file and stay in the Python interpreter prompt. You could then just call cls() because it's already imported.
Alternatively, you could set the environment variable PYTHONSTARTUP in your batch file:
If this is the name of a readable file, the Python commands in that file are executed before the first prompt is displayed in interactive mode. The file is executed in the same namespace where interactive commands are executed so that objects defined or imported in it can be used without qualification in the interactive session.

my GUI doesn't run as executable using pyinstaller?

I've used pyinstaller to convert my Tkinter GUI to an executable file. When I run the exe through command prompt, it reads it but does nothing (I know it reads it because when it has errors they are shown). I'm very new to programming so I wrote a simpler script to generate a window only, converted this to exe and encountered the same problem. Any fundamental rules I'm not aware of that will correct this? Here's the simple code:
import tkinter
from tkinter import *
import os
import sys
def startup():
global CODES
CODES = Tk()
CODES.title("TEST WINDOW - PYTHON EXECUTABLE")
CODES.geometry("1555x569+20+20")
return CODES.mainloop
startup()
Saved it as "test.py", converted to exe by "pyinstaller test.py -F" in Command Prompt. When run, it just pauses then returns to Command Prompt

Call a python script from another 'frozen' python script

I have a simple script that calls other scripts and works fine:
def demandesparbranche():
os.system('python Sources/x.py')
def demandesparlogiciel():
os.system('python Sources/xx.py')
def demandeshcapprouvees():
os.system('python Sources/xxx.py')
def challengesreussis():
os.system('python Sources/xxxx.py')
My idea was to add a GUI with tkinter and freeze this code (with pyinstaller) and use it as a set of buttons to launch the scripts that would in this way remain modifiable. I tried and it does not work, which is logical since in a computer without python installed the command 'python' is obviously unknown. The code works fine in my computer where python is installed.
Is this in any way possible using possibly another form of script calling? What I mean is: how to call the Python interpreter frozen by pyinstaller instead of a system one?
So, I found the solution:
Instead of calling the script with the 'os' module I imported the needed scripts :
from xscript import x
And called it directly via button:
tk.Button(mainframe, width=25, text="Something", command=x, bg='light grey')\
.grid(column=1, row=1, sticky=W)
2 caveats:
A file name init.py is needed in the same directory; same for the scripts imported.

Resources