Building an Python application with cx_freeze.
from cx_Freeze import setup, Executable
_packages = []
_excludes = []
_include_files = [...]
buildOptions = dict(packages = _packages, enter code here`excludes = _excludes, include_files = _include_files, build_exe = '<app name>')
setup(name = '<app name>',
version = <version>,
description = '<description>',
options = dict(build_exe = buildOptions),
executables = [Executable('<app name>.py',
targetName = '<app name>',
icon = '<app name>.png')])
Attempting to install an application build with cx_freeze on Linux in /usr/bin/ with application resources in /usr/share/.
Of course this results in:
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module name 'encodings'
Current thread 0x0... (most recent call first):...
I think I need to set/change the default location of the lib folder but I have been unable to figure out how to do that. It's entirely possible that I might be on the completely wrong track.
I'm trying to avoid using bbfreeze.
After reviewing the code I believe what I want to do is not possible. The 'lib' directory is hard coded in cx_freeze.
Related
Running Python 3.6 on Redhat.
I've installed an extremely complicated project. 5 engineers worked on this for years, but they are all gone now, so I've no one to ask.
At the top level of the project:
setup.py
Inside of that:
# Python command line utilities will be installed in a PATH-accessible bin/
entry_points={
'console_scripts': [
'blueflow-connector-aims = app.connectors.aims.__main__:cli',
'blueflow-connector-csv = app.connectors.csv.__main__:cli',
'blueflow-connector-discovery = app.connectors.discovery.__main__:cli',
'blueflow-connector-fingerprint = app.connectors.fingerprint.__main__:cli',
'blueflow-connector-mock = app.connectors.mock.__main__:cli',
'blueflow-connector-nessusimport = app.connectors.nessusimport.nessusimport:cli',
'blueflow-connector-netflow = app.connectors.netflow.__main__:cli',
'blueflow-connector-passwords = app.connectors.passwords.__main__:cli',
'blueflow-connector-portscan = app.connectors.portscan.__main__:cli',
'blueflow-connector-pulse = app.connectors.pulse.pulse:cli',
'blueflow-connector-qualysimport = app.connectors.qualysimport.qualysimport:cli',
'blueflow-connector-sleep = app.connectors.sleep.__main__:cli',
'blueflow-connector-splunk = app.connectors.splunk.__main__:cli',
'blueflow-connector-tms = app.connectors.tms.__main__:cli',
]
},
I ran:
pipenv shell
to create a shell.
If I try a console script:
blueflow-connector-mock
I get:
bash: blueflow-connector-mock: command not found
That goes to bash which is clearly a mistake. I also tried:
python3 blueflow-connector-mock
which gives me:
python3: can't open file 'blueflow-connector-mock': [Errno 2] No such file or directory
How do I activate these console scripts, so they will actually run?
Specs: Python version 3.7.3 (64-bit)
IDE Microsoft Code (latest edition as of 4/23)
Background I am having issues with executing my code with Configparser only on Microsoft code. I installed other IDE's pycharm etc and I do not have any issues. I prefer Microsoft Code interface / debugging so I would prefer to keep using it. I even went an extra step and installed MS-Code on another server and tried to exec my code with the exact same issue. On a side note if you execute the code in debug mode in ms-code it works without a key error.
This issue is really confusing to me and any help would be appreciated
Current structure of Configparser in code
config = configparser.ConfigParser()
config.read('api_config.ini')
#Reads in each of the api URLs to use from the config file
line_item_reporting = config['apis']['line_item_reporting']
line_item_meta = config['apis']['line_item_meta']
package_reporting = config['apis']['package_reporting']
package_meta = config['apis']['package_meta']
io_meta = config['apis']['io_meta']
io_reporting = config['apis']['io_reporting']
Error code
line_item_reporting = config['apis']['line_item_reporting']
File "C:\ProgramData\Anaconda3\lib\configparser.py", line 958, in getitem
raise KeyError(key)
KeyError: 'apis'
api_config.ini
[general]
meta_pull=50
num_loops=10
index_start=0
index_end=20
password=#######
lookback_window=3
[apis]
io_reporting = https://some_url
line_item_reporting = https://some_url
line_item_meta = https://some_url
package_reporting = https://some_url
package_meta = https://some_url
io_meta = https://some_url
The problem is:
trying to use cmd to for setup.py sdist
cmd returns the message:
The expected result is that my module should have been transformed into a distribution and installed to my Python.
Below is the setup.py:
from distutils.core import setup
setup(
name = 'nester',
version = '1.0.0',
py_modules = ['nester'],
author = 'hfpython',
author_email = 'hfpython#headfirstlabs.com',
url = 'http://www.headfirstlabs.com',
description = 'A simple printer of nested lists',
)
I would like to query Windows using a file extension as a parameter (e.g. ".jpg") and be returned the path of whatever app windows has configured as the default application for this file type.
Ideally the solution would look something like this:
from stackoverflow import get_default_windows_app
default_app = get_default_windows_app(".jpg")
print(default_app)
"c:\path\to\default\application\application.exe"
I have been investigating the winreg builtin library which holds the registry infomation for windows but I'm having trouble understanding its structure and the documentation is quite complex.
I'm running Windows 10 and Python 3.6.
Does anyone have any ideas to help?
The registry isn't a simple well-structured database. The Windows
shell executor has some pretty complex logic to it. But for the simple cases, this should do the trick:
import shlex
import winreg
def get_default_windows_app(suffix):
class_root = winreg.QueryValue(winreg.HKEY_CLASSES_ROOT, suffix)
with winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, r'{}\shell\open\command'.format(class_root)) as key:
command = winreg.QueryValueEx(key, '')[0]
return shlex.split(command)[0]
>>> get_default_windows_app('.pptx')
'C:\\Program Files\\Microsoft Office 15\\Root\\Office15\\POWERPNT.EXE'
Though some error handling should definitely be added too.
Added some improvements to the nice code by Hetzroni, in order to handle more cases:
import os
import shlex
import winreg
def get_default_windows_app(ext):
try: # UserChoice\ProgId lookup initial
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\{}\UserChoice'.format(ext)) as key:
progid = winreg.QueryValueEx(key, 'ProgId')[0]
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'SOFTWARE\Classes\{}\shell\open\command'.format(progid)) as key:
path = winreg.QueryValueEx(key, '')[0]
except: # UserChoice\ProgId not found
try:
class_root = winreg.QueryValue(winreg.HKEY_CLASSES_ROOT, ext)
if not class_root: # No reference from ext
class_root = ext # Try direct lookup from ext
with winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, r'{}\shell\open\command'.format(class_root)) as key:
path = winreg.QueryValueEx(key, '')[0]
except: # Ext not found
path = None
# Path clean up, if any
if path: # Path found
path = os.path.expandvars(path) # Expand env vars, e.g. %SystemRoot% for ext .txt
path = shlex.split(path, posix=False)[0] # posix False for Windows operation
path = path.strip('"') # Strip quotes
# Return
return path
I'm newbie in cx_freeze. I'm triing to make an executable from python 3.3 script that uses "time", "serial" and "tkinter".
Cx_freeze run without any errors, but starting the exe file is resulting with error:
cannot import traceback module
Exception: No module named 're'
Original Exception: No module named 'serial'
I have this setup.py of cx_freeze
from cx_Freeze import setup, Executable
includes = ["serial", "tkinter"]
excludes = []
packages = []
path = []
GUI2Exe_Target_1 = Executable(
# what to build
script ='test6.1.py',
initScript = None,
base = 'Win32GUI',
targetDir = r"dist",
targetName = "bludiste2.exe",
compress = True,
copyDependentFiles = True,
appendScriptToExe = False,
appendScriptToLibrary = False,
icon = None
)
Does anyone know, how to solve it, please?
Thank you.
The first two lines are a bug that will be fixed in the next version of cx_Freeze. If you stick an import re in your script, you'll see the correct error message.
The last line is your real problem - that means it didn't find the serial module when you froze it. Check where pyserial is installed on your computer.