備忘録

備忘録

import_moduleを利用したプログラムをPyInstallerを利用して実行ファイルを出力する方法

Ⅰ. はじめに

import_moduleを利用したプログラムをPyInstallerで実行ファイルを生成し、実行すると以下のエラーが出力されます。
結論からすると「--hidden-import」オプションを指定する必要があります。

> main.exe
Traceback (most recent call last):
  File "main.py", line 4, in <module>
  File "importlib\__init__.py", line 127, in import_module
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'modules'
[17304] Failed to execute script main

Ⅱ. やり方

1. サンプルプログラムを書く

main.py

import importlib

moduleName = "modules.001"
module = importlib.import_module(moduleName)

module.helloworld()

modules/001.py

def helloworld():
  print('helloworld')
2. 実行ファイルを生成する
pyinstaller main.py --onefile --hidden-import="modules.001"
実行結果
> cd dist
> main.exe
helloworld

Pythonで実行ファイルを出力する方法

Ⅰ. はじめに

タイトルの通り「Pythonで実行ファイルを出力する方法」です。

Ⅱ. やり方

1. サンプルプログラムを書く

main.py

print("helloworld")
2. PyInstallerをインストールする
pip install pyinstaller
3. 実行ファイルを出力する
pyinstaller main.py --onefile

実行結果

> cd dist
> main.exe
helloworld

Visual Studio 2019のC++/CLIでWindows Formを利用する方法

Ⅰ. はじめに

タイトルの通り「Visual Studio 2019のC++/CLIWindows Formを利用する方法」です。

Ⅱ. やり方

1. Visual Studio インストーラーを起動する
2. v142 ビルド ツール用 C++/CLI サポートをインストールする

f:id:kagasu:20210426143941p:plain

3. 新規C++/CLIプロジェクトを作成する
4. Windows フォームを追加する

f:id:kagasu:20210426144125p:plain

5. ファイルを編集する

MyForm.cpp

#include "MyForm.h"

using namespace System;
using namespace System::Windows::Forms;

[STAThreadAttribute]
void main(array<String^>^ args) {
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false);
    YOUR_PROJECT_NAME::MyForm form;
    Application::Run(% form);
}
6. Visual Studioを再起動する
7. MyForm.hをダブルクリックする

実行結果

デザイナーが表示され、Windows Formを利用する準備が出来た。
f:id:kagasu:20210426144429p:plain

右クリックメニューの「右に回転」「左に回転」を消す方法

Ⅰ. はじめに

タイトルの通り「右クリックメニューの「右に回転」「左に回転」を消す方法」です。

Ⅱ. やり方

1. コマンドプロンプトで以下コマンドを実行する
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SystemFileAssociations\.png\ShellEx\ContextMenuHandlers\ShellImagePreview" /f
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SystemFileAssociations\.jpg\ShellEx\ContextMenuHandlers\ShellImagePreview" /f
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SystemFileAssociations\.tif\ShellEx\ContextMenuHandlers\ShellImagePreview" /f
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SystemFileAssociations\.tiff\ShellEx\ContextMenuHandlers\ShellImagePreview" /f
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SystemFileAssociations\.webp\ShellEx\ContextMenuHandlers\ShellImagePreview" /f

実行結果

Before After
f:id:kagasu:20210424151404p:plain f:id:kagasu:20210424151413p:plain

右クリックメニューの「デスクトップの背景として設定」を消す方法

Ⅰ. はじめに

タイトルの通り「右クリックメニューの「デスクトップの背景として設定」を消す方法」です。

Ⅱ. やり方

1. コマンドプロンプトで以下コマンドを実行する
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SystemFileAssociations\.png\Shell\setdesktopwallpaper" /f
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SystemFileAssociations\.bmp\Shell\setdesktopwallpaper" /f
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SystemFileAssociations\.gif\Shell\setdesktopwallpaper" /f
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SystemFileAssociations\.heic\Shell\setdesktopwallpaper" /f
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SystemFileAssociations\.heics\Shell\setdesktopwallpaper" /f
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SystemFileAssociations\.heif\Shell\setdesktopwallpaper" /f
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SystemFileAssociations\.heifs\Shell\setdesktopwallpaper" /f
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SystemFileAssociations\.jfif\Shell\setdesktopwallpaper" /f
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SystemFileAssociations\.jpg\Shell\setdesktopwallpaper" /f
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SystemFileAssociations\.tif\Shell\setdesktopwallpaper" /f
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SystemFileAssociations\.tiff\Shell\setdesktopwallpaper" /f
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SystemFileAssociations\.wdp\Shell\setdesktopwallpaper" /f

実行結果

Before After
f:id:kagasu:20210424144327p:plain f:id:kagasu:20210424144357p:plain