How can I create the minimum size executable with pyinstaller? - python

I am on Windows 10, I have anaconda installed but I want to create an executable independently in a new, clean minimal environment using python 3.5. So I did some tests:
TEST1:
I created a python script test1.py in the folder testenv with only:
print('Hello World')
Then I created the environment, installed pyinstaller and created the executable
D:\testenv> python -m venv venv_test
...
D:\testenv\venv_test\Scripts>activate.bat
...
(venv_test) D:\testenv>pip install pyinstaller
(venv_test) D:\testenv>pyinstaller --clean -F test1.py
And it creates my test1.exe of about 6 Mb
TEST 2: I modified test1.py as follows:
import pandas as pd
print('Hello World')
I installed pandas in the environment and created the new executable:
(venv_test) D:\testenv>pip install pandas
(venv_test) D:\testenv>pyinstaller --clean -F test1.py
Ant it creates my test1.exe which is now of 230 Mb!!!
if I run the command
(venv_test) D:\testenv>python -V
Python 3.5.2 :: Anaconda custom (64-bit)
when I am running pyinstaller I get some messages I do not understand, for example:
INFO: site: retargeting to fake-dir 'c:\\users\\username\\appdata\\local\\continuum\\anaconda3\\lib\\site-packages\\PyInstaller\\fake-modules'
Also I am getting messages about matplotlib and other modules that have nothing to do with my code, for example:
INFO: Matplotlib backend "pdf": added
INFO: Matplotlib backend "pgf": added
INFO: Matplotlib backend "ps": added
INFO: Matplotlib backend "svg": added
I know there are some related questions:
Reducing size of pyinstaller exe, size of executable using pyinstaller and numpy
but I could not solve the problem and I am afraid I am doing something wrong with respect to anaconda.
So my questions are:
what am I doing wrong? can I reduce the size of my executable?

I accepted the answer above but I post here what I did step by step for complete beginners like me who easily get lost.
Before I begin I post my complete test1.py example script with all the modules I actually need. My apologies if it is a bit more complex than the original question but maybe this can help someone.
test1.py looks like this:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.image as image
import numpy as np
import os.path
import pandas as pd
import re
from matplotlib.ticker import AutoMinorLocator
from netCDF4 import Dataset
from time import time
from scipy.spatial import distance
from simpledbf import Dbf5
from sys import argv
print('Hello World')
I added matplotlib.use('Agg') (as my actual code is creating figures)
Generating a PNG with matplotlib when DISPLAY is undefined
1) Install a new version of python independently from anaconda.
downloaded python from:
https://www.python.org/downloads/
installed selecting 'add python to path' and deselecting install launcher for all users (I don't have admin rights)
check that I am using the same version from CMD, just writing python I get:
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017,
06:04:45) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
2) Create and activate the environment, from CMD
D:\> mkdir py36envtest
...
D:\py36envtest>python -m venv venv_py36
...
D:\py36envtest\venv_py36\Scripts>activate.bat
3) Install in the environment all the modules needed in the script
Making sure they are compatible to the python version with the command:
(from Matplotlib not recognized as a module when importing in Python)
(venv_py36) D:\py36envtest> python -m pip install nameofmodule
NB: in my case I also had to add the option --proxy https://00.000.000.00:0000
for the example I used development version of py installer:
(venv_py36) D:\py36envtest> python -m pip install https://github.com/pyinstaller/pyinstaller/archive/develop.tar.gz
and the modules: pandas, matplolib, simpledbf, scipy, netCDF4. At the end my environment looks like this.
(venv_py36) D:\py36envtest> pip freeze
altgraph==0.15
cycler==0.10.0
future==0.16.0
macholib==1.9
matplotlib==2.1.2
netCDF4==1.3.1
numpy==1.14.0
pandas==0.22.0
pefile==2017.11.5
PyInstaller==3.4.dev0+5f9190544
pyparsing==2.2.0
pypiwin32==220
python-dateutil==2.6.1
pytz==2017.3
scipy==1.0.0
simpledbf==0.2.6
six==1.11.0
style==1.1.0
update==0.0.1
4) Create/modify the .spec file (when you run pyinstaller it creates a .spec file, you can rename).
Initially I got a lot of ImportError: DLL load failed (especially for scipy) and missing module error which I solved thanks to these posts:
What is the recommended way to persist (pickle) custom sklearn pipelines?
and the comment to this answer:
Pyinstaller with scipy.signal ImportError: DLL load failed
My inputtest1.spec finally looks like this:
# -*- mode: python -*-
options = [ ('v', None, 'OPTION')]
block_cipher = None
a = Analysis(['test1.py'],
pathex=['D:\\py36envtest', 'D:\\py36envtest\\venv_py36\\Lib\\site-packages\\scipy\\extra-dll' ],
binaries=[],
datas=[],
hiddenimports=['scipy._lib.messagestream',
'pandas._libs.tslibs.timedeltas'],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='test1',
debug=False,
strip=False,
upx=True,
runtime_tmpdir=None,
console=True )
5) Finally make the executable with the command
(venv_py36) D:\py36envtest>pyinstaller -F --clean inputtest1.spec
my test1.exe is 47.6 Mb, the .exe of the same script created from an anaconda virtual environment is 229 Mb.
I am happy (and if there are more suggestions they are welcome)

The problem is that you should not be using a virtual environment and especially not anaconda. Please download default python 32 bit and use only necessary modules. Then follow the steps provided in the links, this should definitely fix it.
Although you created a virtual env, are you sure your spec file is not linking to old Anaconda entries?
If all this fails, then submit a bug as this is very strange.

I had a similar problem and found a solution.
I used Windows terminal preview. This program allows creation of various virtual environments like Windows Power Shell (btw. Linux Ubuntu too. Also, worth noting: you can have many terminals in this program installed and, even, open a few at once. Very cool stuff).
Inside Windows Power Shell in Windows terminal preview I installed all the necessary libraries (like numpy, pandas,re, etc.), then I opened the path to my file and tried to use this command:
pyinstaller --onefile -w 'filename.py'
...but, the output exe didn't work. For some reason, the console said that there is a lack of one library (which I had installed earlier). I've found the solution in mimic the auto-py-to-exe library. The command used by this GUI is:
pyinstaller --noconfirm --onedir --console "C:/Users/something/filename.py"
And this one works well. I reduced the size of my output exe program from 911MB to 82,9MB !!!
BTW. 911MB was the size of output made by auto-py-to-exe.
I wonder how is it possible that no one yet has created a compressor that reads the code, checks what libraries are part of the code, then putting only them inside the compression. In my case, auto-py-to-exe probably loaded all libraries that I ever installed. That would explain the size of this compressed folder.
Some suggest using https://virtualenv.pypa.io/en/stable/ but in my opinion, this library is very difficult, at least for me.

Related

Pyinstaller and hiddenimports: how to force to import a package that doesn't get automatically imported by pyinstaller

I tried to generate a .exe file using pyinstaller.
It works fine, except for fact that one package was not automatically detected and imported by pyinstaller.
Such package, that in this example we will call "packageOfInterest", did not get imported because the developers did not provide an hook.
Reading some documentation I understood that this issue could be easily fixed with the following line to be added in the .spec:
hiddenimports=["packageOfInterest"]
Unfortunately it doesn't work, the "packageOfInterest" was not imported even using such line of code.
So my question is: What I'm still missing in the .spec file?
Below my .spec file that I've been using with success with many applications where the packages could be automatically detected by pyinstaller, therefore this is not the case.
import sys
import os
from kivy_deps import sdl2, glew
from kivymd import hooks_path as kivymd_hooks_path
path = os.path.abspath(".")
a = Analysis(
["MyScript.py"],
# "packageOfInterest" in the "hiddenimports" is the package name
# that pyinstaller could not import automatically
hiddenimports=["kivymd.stiffscroll", "packageOfInterest"],
pathex=[path],
hookspath=[kivymd_hooks_path],
datas = [("media\\", "media\\")],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=None,
noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=None)
exe = EXE(
pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
*[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
debug=False,
strip=False,
upx=True,
name="MyScript",
console=True,
icon='myicon.ico'
)
The location of "packageOfInterest" is at the path:
C:\Users\ASUS\AppData\Local\Programs\Python\Python39\Lib\site-packages
Maybe this path should be specified somewhere (e.g. should be addedd in the "pathex" after the "path" value).
In general would be great to identify a clear method that check for all packages imported via "MyScript.py" that cannot automatically be imported by pyinstaller, and that will force their import.
At the same time would be appreciated to understand how to customized to .spec in order to fix the issue.
thanks in advance
The .exe crashed because one file is not showing. Such file was belonging to the "packageofinterest" (mne)
hmm.. you could try use --collect-data packageofinterest as it seems you are missing some files which are belongs to the package.
Also you can use --hidden-import packageofinterest if you don't want to use the spec file.
This question seems old but I hope it helps someone in the future. I recently tried to create a GUI Bundling app. that circumvents as many imported module related issues when I encountered similar interest of forcing PyInstaller to pickup module used by any project I try to Bundle into executable. Here's my workaround.
Every Python module has the file and/or path property/ies. This is because every third-party implementation we install for use (via pip, easy_install, etc) is provided as a module(path via file) or a package(path via path). Only those available as a package have access to both file and path properties.
So, let's assume the modules that couldn't be picked up by PyInstaller are zipfile and psutil. The former is a module while the later is a package.
import zipfile # Use any of the import rules.
print( zipfile.__file__ ) # Take a look at where its absolute path resides.
import psutil
print( psutil.__path__ )
print( psutil.__file__ ) # This will return the package's __init__.py file. Definitely not what you want.
Now, add this path to your PyInstaller's command using the --add-data option.
syntax:
pyinstaller --add-data module_absolute_path;destination my_program_startup_file.py
NOTE:
-The spaces around the print statements is only for readability sake.
-The destination is always a '.' for modules and module name for packages Ex: psutil/ [refer PyInstaller documentation for clarity]
Example:
pyinstaller --onefile --clean --add-data C:\Users\USER\AppData\Local\Programs\Python\Python310\Lib\site-packages\psutil;psutil\ my_program_startup_file.py

failed to create executable with pyinstaller and cefpython on Linux (Invalid file descriptor to ICU data)

I have some simple cefpython code opening a url and am trying to create a stand alone executable with pyinstaller:
I copied files from https://github.com/cztomczak/cefpython/tree/master/examples/pyinstaller to a a directry named pyinstaller
I made following minor changes to pyinstaller.spec
+SECRET_CIPHER = ""
...
- ["../wxpython.py"],
+ ["../hello.py"],
...
- icon="../resources/wxpython.ico")
+ )
I can successfully compile my application on windows with python
On the same machine with python 3.5.4 64 bit and following virtualenv:
cefpython3==66.0
future==0.18.2
PyInstaller==3.2.1
pypiwin32==223
pywin32==228
I can also compile windows with python 3.6.4 64 and following virtualenv:
altgraph==0.17
cefpython3==66.0
future==0.18.2
macholib==1.14
pefile==2019.4.18
PyInstaller==3.3.1
pyinstaller-hooks-contrib==2020.9
pypiwin32==223
pywin32==228
pywin32-ctypes==0.2.0
On Linux compilation works as well, but the executable is not operational.
I get following output and error:
CEF Python 66.0
Chromium 66.0.3359.181
CEF 3.3359.1774.gd49d25f
Python 3.5.2 64bit
[1013/180954.001980:ERROR:icu_util.cc(133)] Invalid file descriptor to ICU data received.
Trace/breakpoint trap (core dumped)
version is python 3.5.2 64bit and the virtualenv is:
cefpython3==66.0
pkg-resources==0.0.0
PyInstaller==3.2.1
What could be the cause?
The code, that I try to compile is below:
import platform
import sys
from cefpython3 import cefpython as cef
def check_versions():
ver = cef.GetVersion()
print("CEF Python {ver}".format(ver=ver["version"]))
print("Chromium {ver}".format(ver=ver["chrome_version"]))
print("CEF {ver}".format(ver=ver["cef_version"]))
print("Python {ver} {arch}".format(
ver=platform.python_version(),
arch=platform.architecture()[0]))
assert cef.__version__ >= "57.0", "CEF Python v57.0+ required to run this"
def main(url="https://www.stackoverflow.com"):
sys.excepthook = cef.ExceptHook
check_versions()
settings = {}
switches = {}
browser_settings = {}
cef.Initialize(settings=settings, switches=switches)
cef.CreateBrowserSync(
url=url,
window_title="CEF_HELLO: ",
settings=browser_settings,
)
cef.MessageLoop()
cef.Shutdown()
if __name__ == "__main__":
main()
Addendum: 2020-10-14:
same error on linux with other versions:
so far I tried python 3.5 and 3.7
Is there anybody who successfully created an executable?
I could be, that this just an issue with the example project and its configuration?
As alternative, a solution could be found in PyInstaller bug 5400
Here the steps:
1- download the PyInstaller helper in CEFpython named hook-cefpython3.py from:
https://github.com/cztomczak/cefpython/tree/master/examples/pyinstaller and put in the root directory of your project
2- In that file, replace the line:
from PyInstaller.compat import is_win, is_darwin, is_linux, is_py2
with:
from PyInstaller.compat import is_win, is_darwin, is_linux
is_py2 = False
3- in your PyInstaller .spec file, add the '.' to the hookspath, e.g. hookspath=['.']. I think it is also possible to add it as PyInstaller command line option.
These steps should solve the problem, until CEFPython deliver a correct version of the hook file.
This is not really the answer I would like to accept, but it is at least one solution and contains information, that might lead to a better fix, a better answer.
After debugging with strace I found out, that the executable searches many files like for example icudtl.dat, v8_context_snapshot.bin, locales/* were searched in
'dist/cefapp/cefpython3but were copied todist/cefapp/`
An ugly work around is to do following after compilation
cd dist/cefapp/cefpython3
ln -s ../* .
and the executable works.
I'm sure there is also a nicer non-brute-force solution, but for the time being I wanted to answer in case others are stuck as well
Probably this can be fixed in the spec file but would we need one spec file for linux and one for windows then?
Perhaps there's also an option to tell the excutable to search for these files one level higer?
To solve this, you need to set this in your spec file:
hookspath=[r'YOUR_ENV_SITE_PACKAGES\cefpython3\examples\pyinstaller\']
And then rebuild, you will have things in the right place.
The following steps solved the issue for me on Windows 10, Python 3.9.5 32-bit, PyInstaller 4.3, and CEFPython 66.1:
Download the hook-cefpython3.py file from here and put it into your project root directory.
Run the pyinstaller command as usual but add the --additional-hooks-dir . command line option, so the command will look like this:
pyinstaller --additional-hooks-dir . <main-file.py>
As opposed to other answers here, this anser neither requires changes of hookspath directive in pyinstaller's spec file and, as of now, nor any changes to the downloaded hook-cefpython3.py file.

How to "hide" some .py files into .exe with cx_Freeze?

Env:
Windows 10
python 3.6.6
cx-Freeze 5.0.2
Git hub example
It contails .msi for installing
Example project structure:
/package_name
/some_packet
/__init.py
/module_name.py # for an example contains valiable in code "module_1"
/main.py
/setup.py
/some_module.py # for an example contains valiable "module_2"
/some_other_module.py # for an example contains valiable "module_3"
Example of setup.py(simplified)
import cx_Freeze
cx_Freeze.setup(
name="example",
options={
"build_exe": {
"packages": ["asyncio"],
"include_files": ["static\some_static_file.png"]
},
"bdist_msi": {
"upgrade_code": "{492de237-1853-4599-a707-c283d567699f}"
}
},
executables=[cx_Freeze.Executable("main.py")]
)
Current behavior
For creating .msi install file -> run command python setup.py bdist_msi. It will generate .msi files for installing application.
After installing this application: directory(where application is installed) will contain:
main.exe
lib\some_packet directory
lib\some_packet\module_name.pyc file
other files
There are following statements:
1) From root directory(where application is installed) i start search(via grep -Rna command under Ubuntu guest system, it's just more convenient for me) and valiable module_1 could be found in directories(in lib\some_packet\module_name.pyc) and module_2/module_3 couldn't be found. Details:
(v_decompile) any#any-pc:/mnt/hgfs/shar/example_installed$ grep -Rna "module_1"
lib/some_packet/module_name.pyc:2:B�!]�#dZdS)module_1N)r�rr�PG:\heroes\installer-with-cx_Freeze\sources_of_project\some_packet\module_name.py<module>s
(v_decompile) any#any-pc:/mnt/hgfs/shar/example_installed$ grep -Rna -a "module_2"
(v_decompile) any#any-pc:/mnt/hgfs/shar/example_installed$ grep -Rna -a "module_3"
2) File lib\some_packet\module_name.pyc could be easily converted to original file(without comments) by e.g. python-uncompyle6.
Details:
(v_decompile) any#any-pc:/mnt/hgfs/shar/example_installed$ uncompyle6 lib/some_packet/module_name.pyc
# uncompyle6 version 3.3.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.6 (default, Jul 20 2018, 15:39:05)
# [GCC 4.8.4]
# Embedded file name: G:\heroes\installer-with-cx_Freeze\sources_of_project\some_packet\module_name.py
# Compiled at: 2019-07-07 11:28:50
module_1 = 'module_1'
# okay decompiling lib/some_packet/module_name.pyc
3) (solved with this question) In both points: file contains source path G:\heroes\installer-with-cx_Freeze\sources_of_project\some_packet\module_name.py It confuses me a bit. Application was installed from .msi and (as I understand) should not know about source directories (regarding path) which was used for creating last one.
Questions:
Is there any way to recover some_module.py and some_other_module.py to original files from main.exe?(like it could be done with lib\some_packet\module_name.pyc)
How to "hide" some other files in application into main.exe or somehow avoid converting .pyc to original files.(maybe some properties in cx_Freeze?)
Note:
It should be done with cx_Freeze.
PS: I don't want to create single .exe. I try to find convenient way for specifying which files should be stored in main.exe like it was done with some_module.py and some_other_module.py
PSS: At this moment I see only I way: put all files on main.py level :) But it will look weird for big project.
Quoting How to obfuscate Python source code:
These two methods [using pyobfuscate and distributing bytecode] are really just a deterrent, not a secure way of hiding the code.
If you want something a bit more robust, you should take a look at Nuitka, which compiles Python code to C++, so you can compile that and just distribute the executable. It seems to be broadly compatible with different libraries and different versions of Python.
See also Python Code Obfuscation
This video might help.
It talks about cxfreeze and how you can use cxfreeze to make a excutable, and I know it works for 3.4+ because the video uses python 3.4, but really your method should be fine...
If I understand Your question correctly, this could be answer You want - Python cx_Freeze for two or more python files (modules)

Permanently set Python path for Anaconda within Cygwin

I'm trying to install Anaconda on my Windows 7 machine. I often use cygwin to for my command-line work, and I would like to manage Anaconda from there. I've worked through the graphic installer without any issues, and checked necessary boxes to reset my default path to this install of python. I go ahead to check where python is and initially I get this...
$ which python
/usr/bin/python
From here python works fine...
$ python
Python 2.7.5 (default, Oct 2 2013, 22:34:09)
[GCC 4.8.1] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
But I'm trying to work from anaconda, so I should just need to redefine my path...
$ export PATH=/cygdrive/c/anaconda:$PATH
$ which python
/cygdrive/c/anaconda/python
And now I should be good to go, but when I try and step into python, it just hangs
$ python
Any idea why this might be happening? verbose return, below...
$ python -v
# installing zipimport hook
import zipimport # builtin
# installed zipimport hook
# C:\anaconda\lib\site.pyc matches C:\anaconda\lib\site.py
import site # precompiled from C:\anaconda\lib\site.pyc
# C:\anaconda\lib\os.pyc matches C:\anaconda\lib\os.py
import os # precompiled from C:\anaconda\lib\os.pyc
import errno # builtin
import nt # builtin
# C:\anaconda\lib\ntpath.pyc matches C:\anaconda\lib\ntpath.py
import ntpath # precompiled from C:\anaconda\lib\ntpath.pyc
# C:\anaconda\lib\stat.pyc matches C:\anaconda\lib\stat.py
import stat # precompiled from C:\anaconda\lib\stat.pyc
# C:\anaconda\lib\genericpath.pyc matches C:\anaconda\lib\genericpath.py
import genericpath # precompiled from C:\anaconda\lib\genericpath.pyc
# C:\anaconda\lib\warnings.pyc matches C:\anaconda\lib\warnings.py
import warnings # precompiled from C:\anaconda\lib\warnings.pyc
# C:\anaconda\lib\linecache.pyc matches C:\anaconda\lib\linecache.py
import linecache # precompiled from C:\anaconda\lib\linecache.pyc
# C:\anaconda\lib\types.pyc matches C:\anaconda\lib\types.py
import types # precompiled from C:\anaconda\lib\types.pyc
# C:\anaconda\lib\UserDict.pyc matches C:\anaconda\lib\UserDict.py
import UserDict # precompiled from C:\anaconda\lib\UserDict.pyc
# C:\anaconda\lib\_abcoll.pyc matches C:\anaconda\lib\_abcoll.py
import _abcoll # precompiled from C:\anaconda\lib\_abcoll.pyc
# C:\anaconda\lib\abc.pyc matches C:\anaconda\lib\abc.py
import abc # precompiled from C:\anaconda\lib\abc.pyc
# C:\anaconda\lib\_weakrefset.pyc matches C:\anaconda\lib\_weakrefset.py
import _weakrefset # precompiled from C:\anaconda\lib\_weakrefset.pyc
import _weakref # builtin
# C:\anaconda\lib\copy_reg.pyc matches C:\anaconda\lib\copy_reg.py
import copy_reg # precompiled from C:\anaconda\lib\copy_reg.pyc
# C:\anaconda\lib\traceback.pyc matches C:\anaconda\lib\traceback.py
import traceback # precompiled from C:\anaconda\lib\traceback.pyc
# C:\anaconda\lib\sysconfig.pyc matches C:\anaconda\lib\sysconfig.py
import sysconfig # precompiled from C:\anaconda\lib\sysconfig.pyc
# C:\anaconda\lib\re.pyc matches C:\anaconda\lib\re.py
import re # precompiled from C:\anaconda\lib\re.pyc
# C:\anaconda\lib\sre_compile.pyc matches C:\anaconda\lib\sre_compile.py
import sre_compile # precompiled from C:\anaconda\lib\sre_compile.pyc
import _sre # builtin
# C:\anaconda\lib\sre_parse.pyc matches C:\anaconda\lib\sre_parse.py
import sre_parse # precompiled from C:\anaconda\lib\sre_parse.pyc
# C:\anaconda\lib\sre_constants.pyc matches C:\anaconda\lib\sre_constants.py
import sre_constants # precompiled from C:\anaconda\lib\sre_constants.pyc
# C:\anaconda\lib\locale.pyc matches C:\anaconda\lib\locale.py
import locale # precompiled from C:\anaconda\lib\locale.pyc
import encodings # directory C:\anaconda\lib\encodings
# C:\anaconda\lib\encodings\__init__.pyc matches C:\anaconda\lib\encodings\__init__.py
import encodings # precompiled from C:\anaconda\lib\encodings\__init__.pyc
# C:\anaconda\lib\codecs.pyc matches C:\anaconda\lib\codecs.py
import codecs # precompiled from C:\anaconda\lib\codecs.pyc
import _codecs # builtin
# C:\anaconda\lib\encodings\aliases.pyc matches C:\anaconda\lib\encodings\aliases.py
import encodings.aliases # precompiled from C:\anaconda\lib\encodings\aliases.pyc
import operator # builtin
# C:\anaconda\lib\functools.pyc matches C:\anaconda\lib\functools.py
import functools # precompiled from C:\anaconda\lib\functools.pyc
import _functools # builtin
import _locale # builtin
# C:\anaconda\lib\encodings\cp1252.pyc matches C:\anaconda\lib\encodings\cp1252.py
import encodings.cp1252 # precompiled from C:\anaconda\lib\encodings\cp1252.pyc
# zipimport: found 13 names in C:\anaconda\lib\site-packages\runipy-0.1.0-py2.7.egg
# zipimport: found 144 names in C:\anaconda\lib\site-packages\setuptools-3.6-py2.7.egg
Python 2.7.7 |Anaconda 2.0.1 (64-bit)| (default, Jun 11 2014, 10:40:02) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and htt
Another (possibly related) issue I'm having is needing to reset the path every time I close/open cygwin. I've entered the following text into .bashrc and .profile to try and set the path permanently:
# Set path to python from anaconda install
export PATH=/cygdrive/c/anaconda:$PATH
After opening and closing cygwin, I return to:
$ which python
/usr/bin/python
Could this be related to setting certain system environment variables?
To work with the interactive Python shell in Cygwin I use the -i option.
To get it from the Anaconda install, I used the steps suggested above:
$ export PATH=/cygdrive/c/anaconda:$PATH
$ which python
/cygdrive/c/anaconda/python
Then I launch python within Cygwin with the -i option:
$ python -i
Python 2.7.8 |Anaconda 2.1.0 (64-bit)| (default, Jul 2 2014, 15:12:11) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>>>
The libraries are also working fine. For instance the pandas library (which has been installed through Anaconda) can be imported now.
>>>> import pandas
>>>> pandas.DataFrame
<class 'pandas.core.frame.DataFrame'>
Now to make this permanent I added the path in my bashrc file:
$ echo 'export PATH=/cygdrive/c/anaconda:$PATH' >> .bashrc
$ source .bashrc
$ which python
/cygdrive/c/anaconda/python
I hope this helps.
I too was having an issue getting anaconda set up with my Cygwin / Windows 7 system. What worked was the following:
Edited the ~/.bashrc. Add below to the bottom of file.
export PATH=/cygdrive/c/Anaconda:$PATH
This mapped Cygwin's python to the anaconda distribution as proof by entering which python in Cygwin's console. However, when launching python it would hang up forcing me to ^C out of the command. I found that python -i will launch the interactive python interpreter with no issues.
Path
Regarding your path issue, see Installing anaconda to use with windows on how to fix that. Alternatively, you can just run /cygdrive/c/Anaconda/python.exe to start the Anaconda/windows python and leave the standard python pointing to the cygwin version.
Note that few of the suggestions given on the internet also include the necessary change to PYTHONPATH: if you use the anaconda/windows python, you also want the anaconda python libraries. Binary modules like numpy will surely crash or misbehave if you mix things up. The easiest way to check this is to install a module in cygwin that you do not need in Conda and test whether you can import that from anaconda python. If you can, your path is wrong.
Anaconda python will automatically add c:\Anconda to your PYTHONPATH, but if you have it set to serve CYGWIN python libraries, those will be found before the Anaconda libs. Try
import sys
sys.path
Terminal
However, this is also a terminal issue: Anaconda (Windows) python expects a windows shell and a modern cygwin shell is like an xterm, so very different. Diagnose this by running python --help, if that works but plain python hangs, the interactive prompt is the problem. This means you can run python programs, you just cannot interact with them.
A way around this is to use the cygwin bash shell instead of the cygwin terminal (both are under Cygwin group in the Start menu). They look very similar, but the font in the bash shell is less pretty and you cannot make the window wider than 80 chars (like all other CMD windows, never understood that 'feature'). This is cygwin bash running inside a CMD window, and your anaconda python will be interactive. The cygwin terminal gives you a bash shell running in a putty derivative, which mimics an xterm and cannot be used by anaconda python.
There is another problem with conda's interpretation of cygwin. When you source the activation script for an environment, it prepends e.g.
/C/Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah
/C/Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah/Library/mingw-w64/bin
/C/Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah/Library/usr/bin
/C/Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah/Library/bin
/C/Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah/Scripts
to your PATH. However, these are not proper directories in cygwin.
I fixed this with
ln -s /cygdrive/c /C
and then if you create conda environments with specific versions of python they should work.
Regarding setting your path permanently, edit the .profile in your home directory (it's a hidden file).
.profile (other names are also valid, see the bash man page) contains bash commands. It is executed when bash is started as login shell.
At the bottom of this file, put your set path command:
export PATH=/cygdrive/c/anaconda:$PATH
As for why python is hanging, please provide more information - for example what's the output of python -v?
I'm a little late in coming to this, but I was having the same problem as you #thomasshouler. Try adding the export line below to the end of the .bash_profile and .profile files as well as your .bashrc file (as mentioned above). Make sure to "source .bash_profile" etc for each file.
export PATH=/cygdrive/c/anaconda:$PATH
Once the Cygwin path is updated as mentioned above, you can also add an alias in the .bashrc file:
alias python='python -i'
This will bring up the Anaconda python interpreter in interactive mode from a Cygwin bash prompt
Found that for Anaconda 4, do:
vim /home/[Username]/.bash_profile
Append to the last line:
PATH=$PATH:/cygdrive/c/Users/[Username]/Anaconda3/Scripts/
This solution worked for me as the usual anaconda3/bin could not be found.

PyInstaller failing to include some modules from C:\Python27\Lib

I've been repeatedly making good PyInstaller executables of a Tkinter utility program, and suddenly this morning the resulting executable fails with a "can't import" error for modules in C:\Python27\Lib, such as "timeit" and "bisect".
The script runs fine on its own. Only the executable has problems.
Any ideas what could have changed to cause this behavior? Or how to force a fix?
[EDIT] Here's the specific error reported by the executable:
Traceback (most recent call last):
File "<string>", line 35, in <module>
File "../..\utils\InterpolatedArray.py", line 12, in <module>
import bisect
ImportError: No module named bisect
When I comment-out the use of this module (to bypass the import of bisect), it next fails on an import of timeit. None of these errors occur when running the script itself.
[EDIT2] Pyinstaller creates the directories it needs (./build and ./dist), and has no permission problems. The pyinstaller build completes without error.
[EDIT3] Here's the build command I'm using:
pyinstaller -F MyMainModule.py
Found a fix, if not the cause. Here's my updated build line:
pyinstaller --hidden-import=timeit --hidden-import=bisect -F MyMainModule.py
Still not sure why PyInstaller suddenly forgot how to find these two modules (and only these two modules) among over 20 other modules correctly included in the build.
I encounter similar issues while packaging a Python script imported openpyxl. Here is my solution.
Step 1: install the python module, openpyxl
$ wine python.exe Scripts/pip.exe install openpyxl
Step 2: add the openpyxl path
Append the openpyxl path (~/.wine/drive_c/Python27/Lib/site-packages) to pathex in the Analysis object in the application spec file (e.g.,ProcessSpreadsheet.spec).
a = Analysis(['ProcessSpreadsheet.py'],
pathex=['C:\\Python27\\Scripts', '~/.wine/drive_c/Python27/Lib/site-packages'],
binaries=None,
datas=None,
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
Step 3: rebuild
$ wine pyinstaller.exe ProcessSpreadsheet.spec
Refer to here for the detailed description.
For unix/linux users, make sure the that you are referencing the same packages when compiling as the application do. This issue mainly occurs when using a virtual environment. For that purpose spot the installed package folder and edit the myapp.spec.
Then run
pyinstaller myapp.spec
I found that pyinstaller and PyInstaller differ in this.
Use PyInstaller when non standard modules/libraries should be included in the executable. PyInstaller needs to be installed using pip/pip3 (I'm unning MacOS) Then just run: PyInstaller -F <script.py>

Categories