Convert py file to exe file with pictures - python

im triyng to convert game that i wrote in python with pictures to exe file . if i convert file without pics , its work, but with pictures it does not,I used pyinstaller. Is there any other process with pictures

The main problem is that, your .exe file does not see your images!
First of all, make sure you're using relative paths to your images.
If you did, you should put the images beside your executable file as the same level as your main.py file, for example, if you have a project structure like this :
.
├── assets
│   ├── fonts-folder
│   │   ├── OFL.txt
│   │   └── Rubik-Regular.ttf
│   ├── imag1.jpg
│   └── icon.png
└── src
   └── main.py
After generating file.exe you should put your file.exe into an executable directory, so your project structure will be looked like that :
.
├── executable
│   └── file.exe
├── assets
│   ├── fonts-folder
│   │   ├── OFL.txt
│   │   └── Rubik-Regular.ttf
│   ├── imag1.jpg
│   └── icon.png
└── src
   └── main.py
OR (NOT RECOMMENDED)
you can just put your .exe file beside your main.py file, so your project structure may look like this:
.
├── assets
│   ├── fonts-folder
│   │   ├── OFL.txt
│   │   └── Rubik-Regular.ttf
│   ├── imag1.jpg
│   └── icon.png
└── src
   ├── main.py
   └── file.exe

Related

requirments.txt with actual dependencies

Is there a way to create a requirements.txt file that only contains the modules that my script actually needs?
I usually just do a pip freeze and then remove unused modules.
You can use pipreqs to analyze your project files, and automatically generate a requirements.txt for you.
First step is to install pipreqs. You can do so, by executing the following command in your console:
pip install pipreqs
Then, the only thing you need to do is to execute the command pipreqs, specifying the path where your files are located at. For example, to generate a requirements.txt, based on the modules on the current working directory, you could execute:
pipreqs .
For some other directory:
pipreqs "PATH/TO/YOUR/PROJECT/PYTHON/FILES"
Real World Example
Here's the tree-view of the folder we're going to use as example:
.
├── data
│   └── EXCEL_FILES
│   ├── DIVISION_MAP.xlsx
│   ├── INPUT_CONTAINER.xlsx
│   ├── KITS.xlsx
│   ├── LOADING.xlsx
│   ├── MOQ_CBM_SHIPPING_TYPE.xlsx
│   ├── OUTBOUND_OTM.XLSX
│   ├── PLANT_SOURCE_MAP.xlsx
│   ├── PORT_STATE.xlsx
│   └── SALABLE_STORAGE_LOCATION.xlsx
├── loading
│   ├── __init__.py
│   ├── configs
│   │   ├── Initialization.py
│   │   ├── __init__.py
│   │   ├── config.ini
│   │   └── logconfig.py
│   ├── constants.py
│   ├── read_files.py
│   ├── reports.py
│   └── utils
│   ├── __init__.py
│   ├── date_utils.py
│   ├── file_utils.py
│   └── utils.py
├── logs
│   └── po_opt.log
├── main.py
└── outputs
   ├── 2022-10-03
   ├── 2022-10-04
   ├── 2022-10-05
   ├── 2022-10-06
   ├── 2022-11-23
    ├── 2022-11-24
   └── 2022-11-28
Executing the command pipreqs . from the root path I get the following requirements.txt:
❯ pipreqs .
INFO: Successfully saved requirements file in ./requirements.txt
Output:
holidays==0.17.2
numpy==1.20.1
pandas==1.2.4
python_dateutil==2.8.2
six==1.16.0

Module not found error in pytest when tests folder contains __init__.py

My tests were working a few hours before. But suddenly tests located in root folder/tests are giving me ModuleNotFoundError: No module named 'tests.test_* I imported the tests.test_a in a top level python file and it worked. Any guesses why cant pytest load them?
I am asking this because this is weird that the test folder located inside root folder/some folder/tests also contains __init__.py and they work fine. I read this question and deleted the __init__.py and it worked. But it is confusing as to why the top level tests would not work.
The folder structure is along the lines of:
.
├── authenticate
│   ├── exceptions.py
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── exceptions.cpython-38.pyc
│   │   ├── __init__.cpython-38.pyc
│   │   ├── repository.cpython-38.pyc
│   │   └── use_case.cpython-38.pyc
│   ├── Readme.md
│   ├── repository.py
│   ├── tests
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   └── test_authenticate.py
│   └── use_case.py
├── tests
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── test_authenticate.cpython-38-pytest-5.4.2.pyc
│   ├── test_authenticate.py
As weird as it sounds, I had forgotten to add __init__.py in one of my packages. I don't know why it was causing tests in my top level directory to fail

What's the purpose of package.egg-info folder?

I'm developing a python package foo. My project structure looks like this:
.
├── foo
│   ├── foo
│   │   ├── bar.py
│   │   ├── foo.py
│   │   ├── __init__.py
│   ├── README.md
│   └── setup.py
├── footest
│   ├── test.py
test.py only has 1 line: import foo
In order for test.py to be able to import the package foo I install it with the command pip3 install -e foo.
Now a new folder called foo.egg-info is created under foo/
.
├── foo
│   ├── foo
│   │   ├── bar.py
│   │   ├── foo.py
│   │   ├── __init__.py
│   ├── foo.egg-info
│   │   ├── dependency_links.txt
│   │   ├── PKG-INFO.txt
│   │   ├── requires.txt
│   │   ├── SOURCES.txt
│   │   ├── top_level.txt
│   ├── README.md
│   └── setup.py
├── footest
│   ├── test.py
What's the purpose of this folder? I tried deleting it and test.py still ran properly. Is is just leftover garbage, similar to the .o files when compiling C projects? If so, is there a way to automatically remove it?
The package.egg-info saves meta data about your installed package like version.
It is used when you for example uninstall it, or "pip list" to see what is installed.
you can open it and take a look.

Two implementations of a module with a same name-import problems

I use two libraries built on top of caffe: crf-rnn(https://github.com/torrvision/crfasrnn/tree/master/python-scripts) and hed(https://github.com/s9xie/hed/blob/master/examples/hed/), the former for semantic image segmentation, the latter for contour detection. Finally, I realized how to get them to work together for object tracking, but now I face an embarrassing problem: as both are built on top of caffe, they import the same package, but each with very different content, i.e. crf-rnn uses caffe.Segmenter which hed doesn't have and ed uses caffe.TEST which crf-rnn doesn't have.
Python doesn't allow import of two packages with the same name. I've tried finding a workaround by puting hed in a separate Python file and importing it in the main script, and using as to import caffe as cf for one of the packages, but so far nothing has worked out.
Any suggestions?
EDIT: this is a file called Aux.py
def import_hed_caffe():
import sys,os
caffe_dir = '/home/alex/Downloads/hed/python'
sys.path.insert(0,caffe_dir)
hed_model = 'deploy.prototxt'
hed_pretrained = 'hed_pretrained_bsds.caffemodel'
import caffe as cf
net = cf.Net(hed_model, hed_pretrained, cf.TEST)
return net
This is the main script:
caffe_root = '../caffe-crfrnn/'
sys.path.insert(0, caffe_root + 'python')
import caffe as espresso
import AuxScript
net = espresso.Segmenter(MODEL_FILE, PRETRAINED, gpu=False)
a=AuxScript.import_hed_caffe()
and I get
AttributeError: 'module' object has no attribute 'TEST'
Needless to say, separately everything works fine, so it's just the import
EDIT 2:
./CMakeFiles
./CMakeFiles/pycaffe.dir
./CMakeFiles/pycaffe.dir/caffe
./caffe
./caffe/imagenet
./caffe/proto
./caffe/test
EDIT 3:
├── caffe
│   ├── _caffe.cpp
│   ├── _caffe.so -> /home/alex/Downloads/hed/lib/_caffe.so
│   ├── classifier.py
│   ├── classifier.pyc
│   ├── detector.py
│   ├── detector.pyc
│   ├── draw.py
│   ├── imagenet
│   │   └── ilsvrc_2012_mean.npy
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── io.py
│   ├── io.pyc
│   ├── net_spec.py
│   ├── net_spec.pyc
│   ├── proto
│   │   ├── caffe_pb2.py
│   │   └── __init__.py
│   ├── pycaffe.py
│   ├── pycaffe.pyc
│   └── test
│   ├── test_layer_type_list.py
│   ├── test_net.py
│   ├── test_net_spec.py
│   ├── test_python_layer.py
│   ├── test_python_layer_with_param_str.py
│   └── test_solver.py
├── classify.py
├── CMakeFiles
│   ├── CMakeDirectoryInformation.cmake
│   ├── progress.marks
│   └── pycaffe.dir
│   ├── build.make
│   ├── caffe
│   │   └── _caffe.cpp.o
│   ├── cmake_clean.cmake
│   ├── CXX.includecache
│   ├── DependInfo.cmake
│   ├── depend.internal
│   ├── depend.make
│   ├── flags.make
│   ├── link.txt
│   └── progress.make
├── cmake_install.cmake
├── CMakeLists.txt
├── detect.py
├── draw_net.py
├── Makefile
├── requirements.txt
I have seen your last edit, and I must say that changing/tampering with python sys.path is necessary in your context but not sufficient here: you have to rename one of the caffe packages.
Ex: if the caffe package is a directory called caffe containing a __init__.py file, rename caffe to espresso and in your code simply:
import espresso
(if it's just a caffe.py file, rename to espresso.py although it may be more problematic if there are other modules in the same directory, well worth a try)
BTW: When importing a module, say, xxx, you can know which full filepath it is using by typing:
print(xxx.__file__)
(useful when you have a doubt)
OK, so I found the least sophisticated solution possible: I wrote two scripts, one for crf-rnn producing the blobs that I ran on the full dataset just once and stored the output.
Then I wrote the second script, with hed edge detector that I use every time I detect and track objects.

Accessing data files after distribution using setup.py

I have a multiplatform project in which I required to ship few third party executable/ data files which are not part of python. In the source file I kept it under data directory and from main script calling the executable using this line
trd_prt_exe = os.path.join("tools", "syslinux", "bin", "executable_name")
It works perfectly fine while testing/ developing from source. Problem comes when I distribute the same using setup.py. After installing application using setup.py, I get this error
for path, subdirs, files in os.walk(os.path.join("tools"))
File "/usr/lib/python2.7/os.py", line 276, in walk
names = listdir(top)
TypeError: coercing to Unicode: need string or buffer, NoneType found
Clearly, python could not find my executables under data directory.
How can we access these executables/ data files during development and after distributing it.
Update I
I could have included but simply forgot. Here is my complete project strecture:-
[sundar#arch multibootusb-7.0.0]$ tree
.
├── data
│   ├── multibootusb.desktop
│   └── multibootusb.png
├── LICENSE.txt
├── multibootusb
├── PKG-INFO
├── README.txt
├── scripts
│   ├── admin.py
│   ├── detect_iso.py
│   ├── __init__.py
│   ├── install_distro.py
│   ├── install_syslinux.py
│   ├── isodump.py
│   ├── multibootusb_ui.py
│   ├── qemu.py
│   ├── uninstall_distro.py
│   ├── update_cfg.py
│   └── var.py
├── setup.py
└── tools
├── checking.gif
├── mbr.bin
├── multibootusb
│   ├── chain.c32
│   ├── extlinux.cfg
│   ├── grub.exe
│   ├── memdisk
│   ├── menu.c32
│   ├── menu.lst
│   ├── syslinux.cfg
│   └── vesamenu.c32
├── multibootusb.png
├── syslinux
│   └── bin
│   ├── syslinux3
│   ├── syslinux4
│   ├── syslinux5
│   └── syslinux6
└── version.txt
Here is what I have in setup.py:-
from distutils.core import setup
import os
mbusb_version = open(os.path.join("tools", "version.txt"), 'r').read().strip()
setup(
name='multibootusb',
version=mbusb_version,
packages=['scripts'],
scripts = ['multibootusb'],
platforms = ['Linux'],
url='http://multibootusb.org/',
license='General Public License (GPL)',
author='Sundar',
author_email='feedback.multibootusb#gmail.com',
description='Create multi boot Live linux on a USB disk...',
long_description = 'The multibootusb is an advanced cross-platform application for installing/uninstalling Linux operating systems on to USB flash drives.',
data_files = [("/usr/share/applications",["data/multibootusb.desktop"]),
('/usr/share/pixmaps',["data/multibootusb.png"]),
('multibootusb/tools',["tools/checking.gif"]),
('multibootusb/tools',["tools/mbr.bin"]),
('multibootusb/tools',["tools/version.txt"]),
('multibootusb/tools/multibootusb',["tools/multibootusb/chain.c32"]),
('multibootusb/tools/multibootusb',["tools/multibootusb/extlinux.cfg"]),
('multibootusb/tools/multibootusb',["tools/multibootusb/grub.exe"]),
('multibootusb/tools/multibootusb',["tools/multibootusb/memdisk"]),
('multibootusb/tools/multibootusb',["tools/multibootusb/menu.c32"]),
('multibootusb/tools/multibootusb',["tools/multibootusb/menu.lst"]),
('multibootusb/tools/multibootusb',["tools/multibootusb/syslinux.cfg"]),
('multibootusb/tools/multibootusb',["tools/multibootusb/vesamenu.c32"]),
('multibootusb/tools/syslinux/bin',["tools/syslinux/bin/syslinux3"]),
('multibootusb/tools/syslinux/bin',["tools/syslinux/bin/syslinux4"]),
('multibootusb/tools/syslinux/bin',["tools/syslinux/bin/syslinux5"]),
('multibootusb/tools/syslinux/bin',["tools/syslinux/bin/syslinux6"])]
#('multibootusb/tools',["tools/multibootusb.png"])]
)
The problem what I found is that the main executable script "multibootusb" is available in usr/bin/multibootusb but other data/ third party executables are under /usr/multibootusb/ and other modules/s scripts required by main program multibootusb is under /usr/lib/python2.7/site-packages/scripts. Therefore, the main program is unable to locate my third party data/ executables.
How to overcome this issue? where am I doing wrong?

Categories

Resources