cx_Freeze can't find local package - python

I'm using dash to create a standalone desktop app. I want to use cx_Freeze to create an executable for my app.
Here's the cx_setup.py file:
import sys
from setuptools import find_packages
from cx_Freeze import setup, Executable
options = {
'build_exe': {
'includes': [
'cx_Logging', 'idna', 'CustomModule'
],
'packages': [
'asyncio', 'flask', 'jinja2', 'dash', 'plotly'
],
'excludes': [
'tkinter'
],
'include_files': [
'database.ini'
]
}
}
base = 'console'
if sys.platform == 'win32':
base = 'Win32GUI'
executables = [
Executable('server.py',
base=base,
target_name='App.exe')
]
setup(
name='App',
packages=find_packages(),
version='0.5.0',
description='',
executables=executables,
options=options
)
Here's what the dir looks like:
project
│ venv
│
└──src
│
└───myPackage
│ cx_setup.py
│ Module1.py
│ Module2.py
Module1.py has the following import statement:
from src.myPackage import Module2Class as mc
cx_Freeze has no problem building the exe but when it tries to run it throws the error:
ModuleNotFoundError: No module named 'src.myPackage'
I've tried putting myPackage in cx_setup.py script but it says that the package doesn't exist. I also used a setup.py to install the package using pip install . to my venv.

Try to add an empty __init__.py file in the src directory.
The import statement:
from src.myPackage import Module2Class as mc
implies that the src directory is treated as a Python module, however cx_Freeze will not recognize it as a module if the __init__.py file is missing (even if some IDEs will) and thus will fail to include files from there into the frozen executable.
You can also try to explicitly add src.myPackage to the packages list of the build_exe options:
options = {
'build_exe': {
...
'packages': [
'src.myPackage', ...
],
...
}
}
but this might not work either, for the same reason.

Related

cx_Freeze : Importing modules fails

I would like to compile a Python3 project with cx_Freeze, but no matter what I do I can never import my own .py files.
Here's my directory structure:
projectname/
setup.py
app/
code/
__init__.py
config.py
run.py
run - editeur.py
...
image/
...
level/
...
My setup.py :
import sys, os
from cx_Freeze import setup, Executable
path = sys.path
includes = []
excludes = []
packages = ["app/code"]
includefiles = ["app/image", "app/level"]
optimize = 0
silent = True
options = {"path": path,
"includes": includes,
"excludes": excludes,
"packages": packages,
"include_files": includefiles,
"optimize": optimize,
"silent": silent
}
base = Win32GUI
cible_1 = Executable(
script="app/code/run.py",
)
cible_2 = Executable(
script="app/code/run - editeur.py",
)
setup(
name="project",
version="1.0",
description="blabla",
options={"build_exe": options},
executables=[cible_1, cible_2]
)
The cx_Freeze compilation is going well and I get my 2 executables.
But when I try to launch one, every time I get the same error:
[...]
File "app/code/run.py", line 7, in <module>
import config
ImportError: No module named 'config'
I really have to miss something stupid since I have no problem with the plug-ins.
It may also be a problem of path or something else I don't know...
Anyone know how to help me a little ? Thanks !
EDIT: I've managed to freeze a simplified example based on your directory structure with the following modification of the setup.py script:
path = sys.path + ['app/code']
packages = []
Alternatively, you could also try the following structure (modifying the import paths accordingly):
projectname/
setup.py
config.py
run.py
run - editeur.py
...
image/
...
level/
...

Creating exe from python program that uses more than one .py file

I have a mid-size program written in Python that i wanted to make avalaible on Windows. I tried using cx_Freeze for that with setup.py looking like this:
from cx_Freeze import setup, Executable
import os
os.environ['TCL_LIBRARY'] = "C:\\Users\w1kl4s\AppData\Local\Programs\Python\Python37-32\\tcl\\tcl8.6"
os.environ['TK_LIBRARY'] = "C:\\Users\w1kl4s\AppData\Local\Programs\Python\Python37-32\\tcl\\tk8.6"
executables = [Executable("main.py", base=None)]
packages = [some, packages]
options = {
'build_exe': {
'packages':packages,
},
}
setup(
name = "ProgramNAme",
options = options,
version = "1.0",
executables = executables
)
However, main.py imports other python files from directory, and those files import other ones from directory as well.
Project tree looks something like that
├── main.py
└── src
├── calculatehash.py
├── calculatesizeandhash.py
└── calculatesize.py
And calculatesizeandhash.py imports calculatesize.py and calculatehash.py files.
I don't really want to create more exe files if that's possible(No idea tho)
How should i approach this?

setup.py console_scripts entry point does not resolve import

I have following setup.py:
from setuptools import setup
from distutils.core import setup
setup(
name="foobar",
version="0.1.0",
author="Batman",
author_email="batman#gmail.com",
packages = ["foobar"],
include_package_data=True,
install_requires=[
"asyncio",
],
entry_points={
'console_scripts': [
'foobar = foobar.__main__:main'
]
},
)
Now, the main.py file gets installed and callable by foobar out of console after installation, which is what I wanted. Problem is, main.py has import at line 3 and that does not work.
So my folder structure is as follows
dummy/setup.py
dummy/requirements.txt
dummy/foobar/__init__.py
dummy/foobar/__main__.py
dummy/foobar/wont_be_imported_one.py
I run python3 setup.py bdist being in dummy directory.
Upon running foobar after installation, I get error
File "/usr/local/bin/foobar", line 9, in <module>
load_entry_point('foobar==0.1.0', 'console_scripts', 'foobar')()
[...]
ImportError: No module named 'wont_be_imported_one'.
UPDATE.
__init__.py has content of
from wont_be_imported_one import wont_be_imported_one
wont_be_imported_one.py has from wont_be_imported_one function which I actually need to import.
In Python 3, imports are absolute by default, and so from wont_be_imported_one import ... inside of foobar will be interpreted as a reference to some module named wont_be_imported_one outside of foobar. You need to use a relative import instead:
from .wont_be_imported_one import wont_be_imported_one
# ^ Add this
See PEP 328 for more information.

Install mixture of extension module. pure python module and shared libraries with distutils

I am using cython for building an extension module. The module depends on an external shared library, which is found when the module is built. Further I have some pure Python modules in the same directory.
Can anybody give me an example setup.py for this task ? I have problems getting the extension module, the pure python module and the shared lib in the same directory when calling "python setup.py install".
I found a solution: I have a package dir ABC like
ABC/
__init__.py
A.py
B.pyx
C.so (or C.dll and C.lib on win)
then the following setup.py does the job:
#input-encoding: utf-8
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
setup(
name = "ABC",
packages = ["ABC"],
package_dir = { "ABC" : "." },
ext_package = "ABC",
cmdclass = {'build_ext': build_ext},
package_data = { ".": [ "C.dll"] },
ext_modules = [ Extension("B", sources="B.pyx", libraries="C" ) ]
)
I had to put setup.py in ABC/ and redirect via package_dir = { "ABC" : "." },

Can't figure out how to include *.html files within a site-packages folder

I cannot figure out how to write my setup.py script in order to include *.html files within the installed package.
Here is my attempt:
import os
from setuptools import setup, find_packages
setup(name='django-testapp',
version='0.1',
author='RadiantHex',
license='BSD',
keywords='testapp,django',
packages=['testapp']],
include_package_data=True,
data_files = os.walk('testapp'),
zip_safe = False,
)
The *.html files are contained within the testapp folder.
Any ideas?
Add following 'package_data' argument to the setup():
setup(...,
package_data={
'testapp' : ['testapp/*.html']
}, ...)

Categories