The command in the exe file does not work - python

I have a python script running a command in the console. Everything works fine in the IDE, but after compiling it into an exe using pyinstaller (pyinstaller -F -i "C:\xampp\htdocs\organaizer\1.ico " websockets.py ) the command does not run. Error up VCRUNTIME140.dll' 14.12 is not compatible with this PHP build linked with 14.29 in Unknown on line 0. I notice that the window appears for a second and immediately disappears. I use Python 3.7. An example of a command that does not run in exe
import subprocess
import os
os.system('php artisan websockets:serve')
Tell me what the problem may be.

import subprocess
subprocess.call(['php', 'artisan', 'websockets:serve'])

Related

Pyinstaller -w exe still opening terminals in Windows

I ran this command to convert my script from .py to .exe:
pyinstaller.exe -w -i .\icon.png --add-data='icon.png;.' .\gui_script.py
and after adding in some poorly added packages the GUI opens via my tkinter script. But when I run code using the tkinter window in Windows, every time there's a subprocess.run or os.system function it opens up a new terminal window. Is there any way to suppress these? Or at least make them minimized or not noticeable?
Here's a piece of the gui_script.py that combines two files which opens an external terminal window.
import os
os.system('copy cDNA.fa+ncRNA.fa transcriptome.fa /b')
I started using python tools for the merging of files:
with open('transcriptome.fa','wb') as transcriptome_file:
for fasta_file in ['cDNA.fa','ncRNA.fa']:
with open(fasta_file,'rb') as current_fasta:
shutil.copyfileobj(current_fasta, transcriptome_file)
as well as downloading of larger files:
with requests.get('http://ftp.ensembl.org/pub/current_fasta/'+species+'/cdna/'+cDNA_file_name, stream=True) as cDNA_request:
with open('cDNA.fa.gz', 'wb') as cDNA_gz_file:
shutil.copyfileobj(cDNA_request.raw, cDNA_gz_file)
Despite this I still need to run an external program, blast, so there I used subprocess.run with the creationflag = subprocess.CREATE_NO_WINDOW argument like this:
if os.name == 'nt':
blast_db_run = subprocess.run(['makeblastdb',
'-in', fasta_file,
'-dbtype', 'nucl',
'-out','blast_db'],
capture_output=True,
creationflags = subprocess.CREATE_NO_WINDOW)
Used the if statement since creationflags doesn't work in a non-windows environment apparently.

'pdflatex' not found in subprocess within a bundled PyInstaller APP

I am attempting to create a macOS standalone app from a PyQt5 GUI using PyInstaller. All works apart from automatically generating a PDF from a TEX file using the pdflatex module (in conjunction with Pylatex).
Both the pylatex and pdflatex modules require calling the subprocess module, which is done as following:
fp = subprocess.run(args, input=self.latex, env=env, timeout=15, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Where args=['pdflatex', '-output-directory=/Users/Desktop', '-interaction-mode=batchmode', '-jobname=test']
This however does not work within the bundled app as it is crashing with the following error which I am logging to a file:
[Errno 2] No such file or directory: 'pdflatex'
Evidently, the pdflatex executable isn't being included/ found in the bundle. Running which pdflatex in the terminal outputs the following directory: /Library/TeX/texbin/pdflatex So I have tried to add the path with the following PyInstaller command:
pyinstaller --noconsole --onefile --path "/Library/TeX/texbin" main.py
But the app still cannot find the pdflatex executable at runtime, whereas the terminal executable works without issues. I have exhausted all similar posts online and ran out of things to try so I was hoping someone might guide me to the solution.
Related info:
macOS version: 10.13.6 High Sierra
Python: 3.8
PyInstaller: 5.1
I had the same problem and I just solved it by editing the runtime environment generated by PyInstaller. In my case the npm command was not found and after running which npm in terminal I could see that npm was located in /usr/local/bin, path that is not included by PyInstaller on macOS.
So my solution is to run this at app startup:
local_bin = '/usr/local/bin'
if local_bin not in os.environ["PATH"]:
os.environ["PATH"] += os.pathsep + local_bin

Run .py script via sh import module error

This is a very basic question on how to code in python and run your script from a very beginner.
I'm writing a script using Xcode9.4.1 which is supposed to be for python3.6. I then have an sh script run.sh, in the same folder of the script (say "my_folder") which simply looks like
python my_script.py
The python script looks like
from tick.base import TimeFunction
import numpy as np
import matplotlib.pyplot as plt
v = np.arange(0., 10., 1.)
f_v = v + 1
u = TimeFunction((v, f_v))
plt.plot(v, u.value(v))
print('donne!\n')
But as I try to run my_script.sh from the terminal I get a "ImportError: No module named tick.base" error.
But the tick folder is actually present in "my_computer/anaconda3/lib/python3.6/site-packages" and up to last week I was using Spyder from anaconda navigator and everything was correctly working, so no "import error" occurred.
The question is quite trivial, in some sense it simply is "what's the typical procedure to code and run python script and how modules are supposed to be imported-downloaded when running on a given machine?"
I need it since my script is to be run on another machine through ssh and using my laptop to make some attempts. Up to last year I used to work in C and only need to move some folders with code and .h files.
Thank for help!
EDIT 1:
From the Spyder 3.2.7 setting, where the script was giving non problem, I printed the
import sys
print(sys.path)
The -manually- copied the content to the sys.path variable in my_script.py and rerun 'run.sh' and now getting a new (strange) error:
Traceback (most recent call last):
[...]
File "/Users/my_computer/anaconda3/lib/python3.6/site-packages/tick/array/build/array.py", line 106
def tick_double_array_to_file(_file: 'std::string', array: 'ArrayDouble const &') -> "void":
^
SyntaxError: invalid syntax
First, check the python which you are calling the script with is pointing to the anaconda python and it is of the same version you are expecting it to be. You can do "which python" command in Linux and Mac to which the path which points to python. It if is pointing to some different version or build of python than the one which you are expecting then add the needed path to the system environment PATH variable. In Linux and Mac this can be done by adding the following line in the .bashrc file at the /home/ folder:
export PATH=/your/python/path:$PATH
And then source the .bashrc file.
source .bashrc
If you are on a operating system like cent os ,breaking the default python path can break your yum so be careful before changing it.
I am running a script in PyCharm and under the Project Interpretor I have the path
C:\envs\conda\keras2\python.exe
When I try to run the script via ssh on the server I get a 'no module named' error. I get
/usr/bin/python as the ans to 'which python' on the server itself. Could you tell me which path I must add for the script to run properly?

Running mayapy on mac

I am trying to run a python script using maya's python interpreter. I am writing this script to be placed in a pipeline, so that maya runs in batchmode. Nothing is happening when I run this command:
maya -batch -script maya.py $1
I get the following message after running this command:
I also tried using the python interpreter directly with a test file
/Applications/Autodesk/maya2017/Maya.app/Contents/bin/mayapy test.py
test.py looks like this
`import maya.standalone
try:
maya.standalone.initialize()
except:
print "standalone already running"`
I get this error ImportError: No module named cmds
I have looked at this post, but it did not help me. What am I doing wrong?

Any pyinstaller detailed example about hidden import for psutil?

I want to compile my python code to binary by using pyinstaller, but the hidden import block me. For example, the following code import psutil and print the CPU count:
# example.py
import psutil
print psutil.cpu_count()
And I compile the code:
$ pyinstaller -F example.py --hidden-import=psutil
When I run the output under dist:
ImportError: cannot import name _psutil_linux
Then I tried:
$ pyinstaller -F example.py --hidden-import=_psutil_linux
Still the same error. I have read the pyinstall manual, but I still don't know how to use the hidden import. Is there a detailed example for this? Or at least a example to compile and run my example.py?
ENVs:
OS: Ubuntu 14.04
Python: 2.7.6
pyinstaller: 2.1
Hi hope you're still looking for an answer. Here is how I solved it:
add a file called hook-psutil.py
from PyInstaller.hooks.hookutils import (collect_data_files, collect_submodules)
datas = [('./venv/lib/python2.7/site-packages/psutil/_psutil_linux.so', 'psutil'),
('./venv/lib/python2.7/site-packages/psutil/_psutil_posix.so', 'psutil')]
hiddenimports = collect_submodules('psutil')
And then call pyinstaller --additional-hooks-dir=(the dir contain the above script) script.py
pyinstall is hard to configure, the cx_freeze maybe better, both support windows (you can download the exe directly) and linux. Provide the example.py, In windows, suppose you have install python in the default path (C:\\Python27):
$ python c:\\Python27\\Scripts\\cxfreeze example.py -s --target-dir some_path
the cxfreeze is a python script, you should run it with python, then the build files are under some_path (with a lot of xxx.pyd and xxx.dll).
In Linux, just run:
$ cxfreeze example.py -s --target-dir some_path
and also output a lot of files(xxx.so) under some_path.
The defect of cx_freeze is it would not wrap all libraries to target dir, this means you have to test your build under different environments. If any library missing, just copy them to target dir. A exception case is, for example, if your build your python under Centos 6, but when running under Centos 7, the missing of libc.so.6 will throw, you should compile your python both under Centos 7 and Centos 6.
What worked for me is as follows:
Install python-psutil: sudo apt-get install python-psutil. If you
have a previous installation of the psutil module from other
method, for example through source or easy_install, remove it first.
Run pyinstaller as you do, without the hidden-import option.
still facing the error
Implementation:
1.python program with modules like platform , os , shutil and psutil
when i run the script directly using python its working fine.
2.if i build a binary using pyinstaller. The binary is build successfully. But if i run the binary iam getting the No module named psutil found.I had tried several methods like adding the hidden import and other things. None is working. I trying it almost 2 to 3 days.
Error:
ModuleNotFoundError: No module named 'psutil'
Command used for the creating binary
pyinstaller --hidden-import=['_psutil_linux'] --onefile --clean serverHW.py
i tried --additional-hooks-dir= also not working. When i run the binary im getting module not found error.

Categories

Resources