Call a python script from another 'frozen' python script - python-3.x

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.

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.

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.

How to create a Python Executable that can run other python-files stored in a folder?

Is it possible to create a Python Executable file (using PyInstaller or similar) that can in its code access other Python-files stored in a specific folder?
The reason for that it MUST be an Executable is that the script sometimes must be run from computers that has not it's own Python installed. I have no problem creating an executable (with PyInstaller for example) and they work fine. The python script itself loads various kinds of data into a database. But everytime there is a new kind of data that has to be loaded into the database I have to recreate the hole exe-file. I'm looking for a way for the executable to access python-files (with Data Load Instructions) so that the actual pyton load-files can be altered instead of the exe-file.
I have tried to find a solution using this:
import os, time
from subprocess import call
for file in os.listdir('.'):
if file == 'loadInstructions.py':
call(['python', file])
print(file)
cwd = os.getcwd()
print(cwd)
input('To EXIT progran press ENTER.')
time.sleep(3)
It works running it from a python editor but when I turn this into an exe-file it does not. If I creat an exe-file with "call(['python', file])" commented out the rest works which I interpret that the exe-file can find the file in question but not run it.
I would be gratefule for any help.

threading.Lock() not working through script

I'm experimenting with the threading function in python 3 to get my own pingtesting app/log working, so im following a youtube tutorial
When I've launched a python 3 interpreter, and run:
>>> import threading
>>> print_lock = threading.Lock()
It correctly returns
>>> print_lock
<_thread.lock object at 0x042093C8>
But when I use that piece of code in a script and try to run it as
python scriptName.py
I get an error saying the attribute Lock() doesn't exist
AttributeError: 'module' object has no attribute 'Lock'
How is this possible? I've verified what threading.Lock() returns when running the python interpreter, why isn't it recognized when I try to run it in a script and how can I get this running?
Did you happen to name your module (or another module in the working directory) threading.py? It would get imported ahead of the built-in threading, causing this exact problem.
Trying running:
print(threading.__file__)
in your module, I suspect you'll find it's not the Python built-in.

Python3 interactive mode on Linux launches the codes twice

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()

Resources