I'm just starting out in python programming and I've noticed after running my python script python text.py few times, there are no .pyc files created. I understand .pyc are only created if its imported but how does text.py still run from command line if there is no .pyc file?
To expand on Rufflewind's answer: the rationale for not writing a .py for start-up scripts is twofold. First, when files are being developed, writing a .pyc file wastes startup time when the file will be ignored on the next run. Second, if a script is large enough that compile speed is noticeable and might matter, stable code can and perhaps should be moved to an imported module. Indeed, for production use, it is standard practice to reduce a startup script to two statements, an import and main() call, as in idlelib.__main__.
""
IDLE main entry point
Run IDLE as python -m idlelib
"""
import idlelib.PyShell
idlelib.PyShell.main()
Related
Let's say that I already built a program that essentially takes some images from an user's path and make new ones using the following libraries/modules:
os itertools pandas PIL
What I need now is to make that program become an executable file that can be actually executed without having to have installed the Python environment and the libraries used in the code, because the user would not know how to code, and would not likely matter how the code works.
The program would run on Windows OS (PC) only , and would use the cmd.exe as the interpreter and medium for user inputs and results display.
Solved here
You can use wheel, and ship whole environment.
#Vincent Caeles:
"#rh979 a wheel is not meant to have all dependencies. Subsequently you could do pip install path/to/wheel.whl --target /path/to/some/folder and zip the contents of the 'folder' to have all your dependencies in the zip archive and ship that to the environment where you want to run your code.
"
Another options is pyinstaller library, according to documentation it should do what you need.
I am currently working on a demo project while learning python and decided to use Tkinter as a user interface for my system. After finishing on the GUI and setting up a MySQL database I tried to use pyinstaller and py2exe to create a standalone .exe but it creates only one executable for one python file and I have 6 python files.
After research I found that most people with multiple python scripts have one main.py file that runs all their scripts and they use this to create a standalone .exe. So since this seems to be the solution but my question is, what is this main.py file and how do I write it so that when I create a .exe it runs my program and still has all the scripts working?
all my python scripts
The first script that I want to execute when run my program is Login.py
Im trying to create a python script with executes a series of python and sql scripts.
Some need to be executed using one python executable and some using another.
I've tried
from subprocess import call
call([r"C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\pythonw.exe", r"C:\Path\to\python\file\blabla.py"])
And
call([r"cd C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3", "pythonw.exe", r"C:\Path\to\python\file\blabla.py"])
But the script isnt being executed.
I think the problem might be "How to execute an .exe file in windows terminal including full path to it"?
Please check my code. It is working well in my side.
from subprocess import call
call([r"C:\Python38\python.exe", r"E:\python\hello.py"])
As i think, the problem is that you use pythonw.exe.
if you use pythonw.exe, you can see any log in terminal.
Pythonw is useful for running script with GUI.
I need to run a script inside a Popen inside a packaged project (with pyinstaller). The command NEEDS to run in python3 and I (obviously) need it to be portable, which means that I can't rely on the config (python/python3 notation) used on the machine I've used to generate the package neither the one where I am using it (I can't also guarantee that there will be any python in the deployment machine...).
I have computers where python is already the correct version, and others where the correct one is python3. I tried to insert #!/usr/bin python3 in the beginning of the file and then run as python but it didn't work.
subprocess.Popen(['python', 'internal.py', arg1, arg2], universal_newlines=True)
In the non-frozen environment I am able to run using sys.executable instead of python. I tried to "pyinstall" the internal.py then, "pyinstall" the rest copying the internal folder to the new folder and then call:
subprocess.Popen(['./internal/internal', arg1, arg2], universal_newlines=True)
But this doesn't work... In the console (moving to the bigger project folder) it does, but when running the package, it doesn't...
Any idea?
Ps. I can't just import the script as a class or whatever. I can't modify system environment vars (except the ones inside the pyinstaller thing...)
I saw something about the PyInstaller.compat that has a exec_python and a __wrap_python, but I couldn't use it I get errors when trying to run the package due to pkg_resources.DistributionNotFound: The 'PyInstaller' distribution was not found and is required by the application, I tried to create a hook, to add as hidden import... None worked (but the hook part maybe I got it wrong...)
I'm augmenting a Python module with a .pxd file so that I can use it pure Python mode for debugging (with pudb). So I have two files:
mymodule.py
mymodule.pxd
I'm using pyximport in the standard way:
import pyximport; pyximport.install()
But pyximport doesn't seem to try to compile it. If I get rid of the .pxd file and rename my .py file to .pyx then it works. So, how can I use pyximport but still be able to run in pure Python mode? Ideally I'd like to be able to simply comment out the pyximport like to switch to pure python mode.
try pyximport.install(pyimport=True), you can see the full document of this function by type pyximport.install? in ipython if installed. Or check the source: https://github.com/cython/cython/blob/master/pyximport/pyximport.py
WARNING: any .py file imported after pyximport.install(pyimport=True) will be compiled, this may result in some errors, eg: __init__.py is compiled to an empty pyd, then the import will fail
pyximport is useful for some simple tests and can be very tricky for complex builds, it should be avoided in real projects.