Okay so I'm a complete noob in programming and I'm trying to compile a simple program I wrote that takes in a string and prints out the string in morse code it's called morse.py. I installed pyinstaller using
pip install pyinstaller
and I am trying to compile my program using pyinstaller.
Now I've searched a bit and it says that I need to write pyinstaller morse.py, but I don't really know where to write that. I tried moving to the directory of my program and doing that in CMD but it didn't work. I tried making a python program in the same directory and doing that and that also didn't work. I couldn't find anything very helpful to tell me exactly how to compile the file.
Can someone please help?
I would suggest to first read the Using Pyinstaller section in the documentation of the module itself.
You can also use some tutorials (e.g. Matt Borgerson's one).
In order to recap you should:
write your script and make sure that it works
run from the command line:
~\ pyinstaller your_file_name.py
this command will generate a your_file_name.spec file where you can include all the dll required by your application and any custom settings (Using Spec Files)
once you have decided what to include in your .exe application you can run from the command line
~\ pyinstaller [option1] [option2] your_file_name.py
You can find the full list the options in the documentation. An example could be pyinstaller.exe --onefile --windowed --icon=app.ico app.py where:
--onefile: Create a one-file bundled executable.
--windowed: Parameter to chooseif you are compiling in Mac OS X or Windows
--icon= : Choose the file to use as icon for file.
You can create your exe file very easily also with py2exe.
Try to write full path to pyinstaller (for example = C:\Users\user\AppData\Local\Programs\Python\Python35-32\Scripts\pyinstaller.exe)
full cmd string must look like:
C:\Users\user\AppData\Local\Programs\Python\Python35-32\Scripts\pyinstaller.exe --onefile myscript.py
I don’t have much experience in Python. I am able to write simple but useful scripts. I was able to convert them to .exe using pyinstaller. But the more programs I write, the more python’s versions I have (and packages and virtual environments). What was working in the beginning suddenly stopped working and got me very confused. But jumping from one page to another I figured it out. My post is to help another newbies. I’m using Windows, but some rules are the same probably for other systems.
I’m writing it in March 2020. Pyinstaller doesn’t work properly with the newest python version which is now 3.8. So firstly you need to install Python 3.7. Secondly choose the version accordingly to type of your system. It can be 32 or 64.
You can install more than one Python’s version. In command line when you type “py” your computer will look for the newest Python’s version - I mean the one with the highest version number. It’s because of the file C:\Windows\py.exe which appears when you install your first Python. If today you have installed only python 3.7, “py” will run Python 3.7. But next day when you install Python 3.8 and you type “py” it will run Python 3.8. If you want to use the older one just type py -3.7
C:\Users\Ania>py
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
C:\Users\Ania>py -3.7
Python 3.7.7 (tags/v3.7.7:d7c567b08f, Mar 10 2020, 10:41:24) [MSC v.1900 64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Now to work with your project you need to create a virtual environment for it. Remember to use the correct python version. First let’s make a folder for it.
D:\PYTHON>mkdir my_new_great_project
Go to that folder:
D:\PYTHON>cd my_new_great_project
Create the venv:
D:\PYTHON\my_new_great_project>py -3.7 -m venv venv
The first “venv” above is the name of a python module which we are using. The second “venv” is the name of the virtual environment which we are creating.
Activate the venv:
D:\PYTHON\my_new_great_project>venv\Scripts\activate.bat
You should see:
(venv) D:\PYTHON\my_new_great_project>
Now you can use pip to install whatever external packages your project needs.
(venv) D:\PYTHON\my_new_great_project>pip install pandas
Pip “knows” that you need packages for python 3.7.
When you create your project files don’t put them in “venv” folder.
To run your project from the venv just type its name
(venv) D:\PYTHON\my_new_great_project>great_project.py
If you are done with your project, everything works perfectly and you need .exe file, it’s time to install pyinstaller in your venv.
(venv) D:\PYTHON\my_new_great_project>pip install pyinstaller
To check what external packages you’ve already installed just type “pip list”:
(venv) D:\PYTHON\my_new_great_project>pip list
Package Version
altgraph 0.17
future 0.18.2
numpy 1.18.2
pandas 1.0.3
pefile 2019.4.18
pip 19.2.3
PyInstaller 3.6
python-dateutil 2.8.1
pytz 2019.3
pywin32-ctypes 0.2.0
setuptools 41.2.0
six 1.14.0
Now from venv you can run pyinstaller to make an .exe file:
(venv) D:\PYTHON\my_new_great_project>pyinstaller --onefile --name my_project great_project.py
Now your project is available as an single .exe file in the folder called “dist”.
I hope it helps someone.
Hello i made a code in python, i used it to turn itself into a exe.
Make sure to have it in same directory as the file you want converted.
import subprocess
import shutil
import os
cmd = input("file name: ") # asks user for filename
extra = input("extra commands? eg -w -F if using two or more put a space between: ") # asks for extra options
suros = "pyinstaller "+ extra + " " + cmd + ".py" # sets run command
current_workin_path = os.getcwd() # gets current working path
dirt = current_workin_path + "/"+ cmd # set current working path for folder creation
os.mkdir(dirt) # creates folder for working path and for a copy
shutil.copy(cmd + ".py", current_workin_path + "/" + cmd + "/" + cmd + ".py") # creats copy of python file in the newly set path
os.chdir(current_workin_path + "/" + cmd + "/") # changes current working path to newly created one
subprocess.run(suros) # runs the command set eariler
os.remove(cmd + ".py") # delete the copy of python file
Hope this helps someone.
Related
I am trying to import pyodbc module on a windows computer. It works in the terminal, but not the IDLE. The error message in IDLE is:
Traceback (most recent call last):
File "FilePath/Filename.py", line 3, in <module>
import pyodbc
ImportError: No module named pyodbc
This typically occurs when multiple versions of python are installed with different paths. You can check to see if you have multiple installations by opening up the IDLE terminal and using
import sys
sys.version
sys.path
These commands will print the system PATH and version of the current instance of python. Use this in both IDLE and the command line terminal to see where each differ. Once you know which version is the one you want then just remove the other. You could also remove all python instances and then reinstall a clean python environment but then you would have to re-install all of your modules using pip or easy_install
Open python in cmd (type python and press enter)
Import the module in cmd (type import modulename)
Type modulename.__file__
You will get the path where the module is stored
Copy the corresponding folder
In IDLE, import sys and typing sys.executable to get the paths where it looks for modules to import
Paste your module's folder in the path where IDLE looks for modules.
This method worked for me.
You can pip show after install package and know about location where package installed.
After that check in IDLE sys.path and if directory with package not in sys.path try to add it.
import sys
sys.path.append("/home/dm/.local/lib/python3.6/site-packages")
# or another folder that `pip show` about package.
this happen because of multiple python installed (32bit version, 64bit version) or 3v and 2.7v so to solve this problem you have to invoke the idle for that specific version like this
cd to the dir of the version that the import work fine in cmd in that folder type this command below
pythonw.exe Lib\idlelib\idle.pyw
this command will invoke idle for that version
and the import will work fine
Me too had the same issue while trying to import a module which was successfully imported on terminal and not able to install on IDLE.
How I fixed?
Assuming you know how to execute commands on terminal as well as inside of python interpreter
Open your Terminal & execute the below commands :
:~$ python3
Python 3.6.9 (default, Jan 26 2021, 15:33:00)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
>>> import sys
>>> sys.version
'3.6.9 (default, Jan 26 2021, 15:33:00) \n[GCC 8.4.0]'
>>> sys.path
['', '/usr/lib/python36.zip', '/usr/lib/python3.6',
'/usr/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/dist-
packages', '/usr/lib/python3/dist-packages']
>>>
Now import your module inside of your python3 interpreter.
>>> import nester
>>>
>>> nester.__file__
'/usr/local/lib/python3.6/dist-packages/nester.py'
>>>
Open your IDLE and run the below commands and compare them
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927
64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more
information.
>>> import sys
>>> sys.version
'3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit
(AMD64)]'
>>> sys.path
['','C:\Users\username\AppData\Local\Programs\Python\Python39\Lib\idlelib', 'C:\Users\username\AppData\Local\Programs\Python\Python39\python39.zip', 'C:\Users\username\AppData\Local\Programs\Python\Python39\DLLs', 'C:\Users\username\AppData\Local\Programs\Python\Python39\lib', 'C:\Users\username\AppData\Local\Programs\Python\Python39', 'C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages']
>>> sys.executable
'C:\Users\username\AppData\Local\Programs\Python\Python39\pythonw.exe'
Now if you compare both outputs from Terminal & IDLE,
Terminal Module location is different from IDLE
I was using Ubuntu 18 terminal on windows machine
So I just copied my file to 'C' directory and ensured its file privileges. That's it.
:~$ cp -p /usr/local/lib/python3.6/dist-packages/nester.py /mnt/c/Users/username/AppData/Local/Programs/Python/Python39/Lib/
It worked!!
I Found the solution. It works for me
The problem is your installation directory does not match with the python version directory.
solution is >>>
type %localappdata% in your search bar then go to this folder.
here select the program folder. then select Programs , Python , Python version , Scripts
copy the location of the Scripts folder
open command prompt and type cd //yourpath (in my case cd C:\Users\3C HOUSE\AppData\Local\Programs\Python\Python37\Scripts)
if you wanna install numpy , now run pip install numpy
When you put your python scripts that have import pandas in the same folder as the site packages like pandas for example and use the same version of python that is used on CMD, it should help run your scripts in IDLE.
Check the path of your code, and that of the module. Copying the module to the path where code is worked for me.
'sys.executable' will give the path where code is stored.
For windows, open command prompt and enter pip show pyodbc to get the path of package and copy the path.
then open idle and run these lines
import sys
sys.path
Match the path from command prompt and the paths mentioned in the list provided by running above lines in IDLE. If the path is not mentioned then run these lines in idle
sys.path.append("Enter the copied path of package here")
After executing these lines, check again by importing the package that if it works for you.
I use cygwin on Windows 10, and want to write Python code
that uses Pandas and other packages contained in my Anaconda base
environment. I can run Python code if I give the absolute
path to the code, but not if bash needs to use PATH
to find the code.
I use these lines in my .bash_profile as suggested
here (search for 'bash_profile'):
. /opt/conda/etc/profile.d/conda.sh
conda activate base
Here's the start of my PATH, showing Anaconda stuff at the front:
$ echo ${PATH:1:160}
cygdrive/c/Users/Glenn/Anaconda3:/cygdrive/c/Users/Glenn/Anaconda3/Library/mingw-w64/bin:/cygdrive/c/Users/Glenn/Anaconda3/Library/usr/bin:/cygdrive/c/Users/Gle...
I can run Anaconda Python at the bash prompt:
$ which python
/cygdrive/c/Users/Glenn/Anaconda3/python
$ python -i
Python 3.7.3 (default, Apr 24 2019, 15:29:51) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Warning:
This Python interpreter is in a conda environment, but the environment has
not been activated. Libraries may fail to load. To activate this environment
please see https://conda.io/activation
Type "help", "copyright", "credits" or "license" for more information.
>>>
I can even run a Python program that uses Pandas:
$ pwd
/home/Glenn/bin
$ cat test.py
#!/usr/bin/env python
import pandas as pd
print(pd.Series([1,2]))
$ ./test.py
0 1
1 2
dtype: int64
The problem is that I can't run the program if I rely on the path:
$ which test.py
/home/Glenn/bin/test.py
$ test.py
C:\Users\Glenn\Anaconda3\python.exe: can't open file '/home/Glenn/bin/test.py': [Errno 2] No such file or directory
$
I get the same kind of error message if I invoke test.py from
another directory using an absolute pathname, but not if I invoke
it using a relative pathname.
I'm a longtime Cygwin user and haven't seen this kind of behavior
before, so suspect it has to do with Anaconda.
I can change to any other directory and get the same behavior.
Also, if I put the absolute path to Anaconda's Python in the
shebang line, I get the same behavior.
I've found (and tried) many suggestions for configuring Anaconda in a Cygwin environment, but many of them are 4 or 5 years old.
UPDATE: I was not able to solve this problem, but have switched to the Linux Windows Subsystem and no longer have to deal with the issue.
I am trying to import pyodbc module on a windows computer. It works in the terminal, but not the IDLE. The error message in IDLE is:
Traceback (most recent call last):
File "FilePath/Filename.py", line 3, in <module>
import pyodbc
ImportError: No module named pyodbc
This typically occurs when multiple versions of python are installed with different paths. You can check to see if you have multiple installations by opening up the IDLE terminal and using
import sys
sys.version
sys.path
These commands will print the system PATH and version of the current instance of python. Use this in both IDLE and the command line terminal to see where each differ. Once you know which version is the one you want then just remove the other. You could also remove all python instances and then reinstall a clean python environment but then you would have to re-install all of your modules using pip or easy_install
Open python in cmd (type python and press enter)
Import the module in cmd (type import modulename)
Type modulename.__file__
You will get the path where the module is stored
Copy the corresponding folder
In IDLE, import sys and typing sys.executable to get the paths where it looks for modules to import
Paste your module's folder in the path where IDLE looks for modules.
This method worked for me.
You can pip show after install package and know about location where package installed.
After that check in IDLE sys.path and if directory with package not in sys.path try to add it.
import sys
sys.path.append("/home/dm/.local/lib/python3.6/site-packages")
# or another folder that `pip show` about package.
this happen because of multiple python installed (32bit version, 64bit version) or 3v and 2.7v so to solve this problem you have to invoke the idle for that specific version like this
cd to the dir of the version that the import work fine in cmd in that folder type this command below
pythonw.exe Lib\idlelib\idle.pyw
this command will invoke idle for that version
and the import will work fine
Me too had the same issue while trying to import a module which was successfully imported on terminal and not able to install on IDLE.
How I fixed?
Assuming you know how to execute commands on terminal as well as inside of python interpreter
Open your Terminal & execute the below commands :
:~$ python3
Python 3.6.9 (default, Jan 26 2021, 15:33:00)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
>>> import sys
>>> sys.version
'3.6.9 (default, Jan 26 2021, 15:33:00) \n[GCC 8.4.0]'
>>> sys.path
['', '/usr/lib/python36.zip', '/usr/lib/python3.6',
'/usr/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/dist-
packages', '/usr/lib/python3/dist-packages']
>>>
Now import your module inside of your python3 interpreter.
>>> import nester
>>>
>>> nester.__file__
'/usr/local/lib/python3.6/dist-packages/nester.py'
>>>
Open your IDLE and run the below commands and compare them
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927
64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more
information.
>>> import sys
>>> sys.version
'3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit
(AMD64)]'
>>> sys.path
['','C:\Users\username\AppData\Local\Programs\Python\Python39\Lib\idlelib', 'C:\Users\username\AppData\Local\Programs\Python\Python39\python39.zip', 'C:\Users\username\AppData\Local\Programs\Python\Python39\DLLs', 'C:\Users\username\AppData\Local\Programs\Python\Python39\lib', 'C:\Users\username\AppData\Local\Programs\Python\Python39', 'C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages']
>>> sys.executable
'C:\Users\username\AppData\Local\Programs\Python\Python39\pythonw.exe'
Now if you compare both outputs from Terminal & IDLE,
Terminal Module location is different from IDLE
I was using Ubuntu 18 terminal on windows machine
So I just copied my file to 'C' directory and ensured its file privileges. That's it.
:~$ cp -p /usr/local/lib/python3.6/dist-packages/nester.py /mnt/c/Users/username/AppData/Local/Programs/Python/Python39/Lib/
It worked!!
I Found the solution. It works for me
The problem is your installation directory does not match with the python version directory.
solution is >>>
type %localappdata% in your search bar then go to this folder.
here select the program folder. then select Programs , Python , Python version , Scripts
copy the location of the Scripts folder
open command prompt and type cd //yourpath (in my case cd C:\Users\3C HOUSE\AppData\Local\Programs\Python\Python37\Scripts)
if you wanna install numpy , now run pip install numpy
When you put your python scripts that have import pandas in the same folder as the site packages like pandas for example and use the same version of python that is used on CMD, it should help run your scripts in IDLE.
Check the path of your code, and that of the module. Copying the module to the path where code is worked for me.
'sys.executable' will give the path where code is stored.
For windows, open command prompt and enter pip show pyodbc to get the path of package and copy the path.
then open idle and run these lines
import sys
sys.path
Match the path from command prompt and the paths mentioned in the list provided by running above lines in IDLE. If the path is not mentioned then run these lines in idle
sys.path.append("Enter the copied path of package here")
After executing these lines, check again by importing the package that if it works for you.
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.
As the offical pyqt5-installation in the ubuntu repositories seem to lack support for QtQuick, I tried to install pyqt5 from source. The installation itself seems to work correctly, but when running a python script that uses PyQt5, python complains that it cannot find that PyQt.
After building sip 4.15.5, I downloaded PyQt5.2. It should be compatible to my version of Qt (output of qmake --version):
QMake version 3.0
Using Qt version 5.2.0 in /opt/qt5.1.1/5.2.0/gcc_64/lib
I ran
The output of configure.py of pyqt can be found here: https://gist.github.com/Mitmischer/8677889 .
The installation output of pyqt can be found here: https://gist.github.com/Mitmischer/8677780 .
After sudo make install, I can see a folder PyQt5 in /usr/lib/python3.3/site-packages which is quite nice. However, if I run cat PyQt5/__init__.py, there is no actual code inside:
# Copyright (c) 2014 Riverbank Computing Limited <info#riverbankcomputing.com>
#
# This file is part of PyQt5.
#
# This file may be used under the terms of the GNU General Public License
# version 3.0 as published by the Free Software Foundation and appearing in
# the file LICENSE included in the packaging of this file. Please review the
# following information to ensure the GNU General Public License version 3.0
# requirements will be met: http://www.gnu.org/copyleft/gpl.html.
#
# If you do not wish to use this file under the terms of the GPL version 3.0
# then you may purchase a commercial license. For more information contact
# info#riverbankcomputing.com.
#
# This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
# WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
Yep, that's all what is inside that file. I don't know whether it's supposed to be that way, but it looks strange to me. Furthermore (ls PyQt5):
__init__.py QtCore.so QtGui.so QtMultimediaWidgets.so QtPositioning.so QtQuick.so Qt.so QtTest.so QtX11Extras.so
_QOpenGLFunctions_2_0.so QtDBus.so QtHelp.so QtNetwork.so QtPrintSupport.so QtSensors.so QtSql.so QtWebKit.so QtXmlPatterns.so
QtBluetooth.so QtDesigner.so QtMultimedia.so QtOpenGL.so QtQml.so QtSerialPort.so QtSvg.so QtWidgets.so uic/
Doesn't look that pythonic.
As suggested elsewhere, I (hopefully) set my pythonpath appropriately:
> echo $PYTHONPATH
/usr/lib/python3.3/site-packages/
Now, if I start an interactive python3.3-session (or a script), PyQt5 cannot be found:
Python 3.3.2+ (default, Oct 9 2013, 14:50:09)
[GCC 4.8.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from PyQt5 import *
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'PyQt5'
>>>
Has anyone else tried to install PyQt5 from source? What can I do to make PyQt work?
You probrably messed PYTHONPATH in someway.
I have successfully built, installed and used PyQT using virtual environment. So here is how to install it using virtualenv. There are tons of tutorals, so please read about it.
So install python-virtualenv, virtualenvwrapper (at least that's what they are called on Debian).
mkvirtualenv -p /path/to/python3.3 name
workon name
cd PyQtSource
configure
make
make install
To use this enviorment do:
workon name
python