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

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.

Related

Importing module from within another Script while using Azure ML

I was earlier using console to run scripts which worked fine. Now that I wish to shift the entire process to crontab, I m facing ModuleNotFoundError:. The structure follows as I run a main file from parent directory, which imports another .py script from different location. To tackle the path error issue I have used os.chdir(path) which seems to work when I give print(os.getcwd()) but fails to find the required scripts in this new location. Can someone please help me with this. I m new to scripting.
#TIA
cwd = os.getcwd()
try:
os.chdir(cwd + 'pathToOtherScripts')
print('#\tGet current directory:',os.getcwd())
import SomeScript as SS
I get ModuleNotFoundError: at the parent directory. This error doesn't happen when I run the same script through console.

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 Run .py file

I have written a "random movie suggester" code that will basically read the files of the directory where the .py file is stored and randomly pick one of them and print it out.
My problem is -
How to make .py file simple to run for non-technical person?
(e.g. I tried .bat but i had to hard code the path of the file).
Other solutions ask to install IDEs and run it.
First you have to install python on your computer.
Then you can type in your terminal:
python [the file name or the path to access it]
But I don't think this is what you are looking for...
You can create a .exe file with your python program using the pyinstaller module then running this in your terminal.
pip pyinstaller
pyinstaller --onefile [the name of the file]
The first line installs the module using pip.
The second line will create a .exe file that does exactly the same as your python code.

Where can i find the pyc file?

The python3 version is Python 3.5.3 in my os.
mkdir workspace
cd workspace
vim print.py
print("i am learning")
Saved and exit.
python3 print.py
i am learning
As far as i knew, python source file was parsed and compiled into pyc file when to execute it.
ls
print.py
There is no pyc file in workspace directory,where is the complied print.py file then?
sudo find / -name ".pyc"
The find command still can't search pyc file such as print.pyc .
python3 -m compileall can create the compiled file for print.py manually,where is the compiled file for print.py created by python itself?
Does python3 delete the print.pyc after executing python3 print.py?
Ok this is one big of a problem I ever had when I'm started to learn python few years back. Python is just like any other oop programming languages which does compilation before program execution. When python compiles its program, it creates the bite code which is you can see by standard library called dis.
import dis
print(dis.dis(your_program))
Sometimes (not always) python creates .pyc file for the running programs to improve the speed up the loading of import modules but not to improve the execution time. So hope you get intuition behind .pyc, furthermore .pyc only creates when your module is import by another module.
As an example, Imagine you have this print.py (Let's modify it shall we)
def return_print_statment(statement):
print('Printed version: ', statement)
Suppose this module imported by another custom module called views.py. In views.py there is a module_view which will use the return_print_statment
from print import return_print_statment
def module_view():
...
return_print_statment(output)
So in the compilation, since you have imported the print.py python will generate print.pyc file for it. In python 2.0 python will put the .pyc to right next to your program in the same folder, but in python3 instead of creating in the same folder python will create separate folder called __pycache__ in the same directory to put these byte codes.
python3 -m compileall .
To compile all .py files in your current directory.
http://effbot.org/pyfaq/how-do-i-create-a-pyc-file.htm

Using a python library (Biopython) from a python program on a different folder that is installed

I generally like to make my python programs in a text editor and then run them after they are complete instead of line arguments. Thus, I save those .py files on a convenient folder location instead of Python program files.
I then run my .py file using Command Prompt. However it has not worked for the Biopython library as import Bio gives back a Traceback No module named 'Bio'. However, using line arguments directly on python shows it is installed.
I have never had this issue with Python in general and other downloaded libraries (import numpy for example works fine). How do I make the files available to be open from any location?? Or how do I provide the path in import?
To clarify and add:
1) I use Python from Windows 10
2) I downloaded and installed Python(3.7) from python.com
3) I downloaded Biopython using pip (and all other libraries I downloaded)
4) I also tried it on Jupyter notebook and also does not work to import Bio, whereas Import numpy does.
Thanks!

Resources