1

I have two python files: Project_1.py is the main file. config.py is the configuration file which contains the location paths. When I run project_1 locally it works perfect. It constantly moves files from location A to Location B exactly as I wanted.

So my goal was to create a python executable of the above program, so I did this: pyinstaller --onefile -w project_1.py in cmd.

So I copied project_1.exe, config_py and the data in LocationA and LocationB to a new environment (OS windows, but without Python):

enter image description here

So I adjusted the paths in config.py with notepad++ to the new paths in the new environment and tried to run project_1.exe by double clicking on it. This is what I got: enter image description here

This is the source code of project_1.py locally:

import os
import shutil
import config as cfg

def move_files(source, destination):   
    files = os.listdir(source)
    for f in files:
        f_path = os.path.join(source, f)
        shutil.move(f_path, destination)

if len(os.listdir(cfg.location_a)) == 0:
    move_files(cfg.location_b, cfg.location_a)
elif len(os.listdir(cfg.location_b)) == 0:
    move_files(cfg.location_a, cfg.location_b)

This is the source code of config.py locally:

location_a = r'C:\Users\FFF\Desktop\Learn Airflow\pythonproject\LocationA'
location_b = r'C:\Users\FFF\Desktop\Learn Airflow\pythonproject\LocationB'

I don't have a clue what is going on, it seems like the config.py does not seem to be recognized but I don't know.

TangerCity
  • 775
  • 2
  • 7
  • 13
  • Does this answer your question? [Importing external module in single-file exe created with PyInstaller](https://stackoverflow.com/questions/47350078/importing-external-module-in-single-file-exe-created-with-pyinstaller) – Kemp Jun 21 '21 at 14:18
  • Alternatively (to explain why relative paths might not work the way you expect): https://stackoverflow.com/questions/67487825/setting-working-directory-of-a-converted-py-file-to-exe-file – Kemp Jun 21 '21 at 14:20
  • No it does not answer my question as my case is more specific. – TangerCity Jun 21 '21 at 14:21
  • In your `project_1.py`, add a `print(cfg.__file__)` just after the import so you can see which file is loaded. It may not be the same `config.py`. –  Jun 21 '21 at 14:24
  • @JustinEzequiel where will it be printed? – TangerCity Jun 21 '21 at 14:25
  • you might have to recompile with the new config.py file I think. The config.py probably got compiled with the exe and it is looking at the old paths. Just a guess though. – ldall03 Jun 21 '21 at 14:26
  • What's more specific about your case? You're trying to import your `config.py` from a script that has been turned into an exectuable. That won't work unless you exclude the `config.py` from your executable build and modify the location it's looking for the `config.py`, exactly as per the linked question. – Kemp Jun 21 '21 at 14:26
  • 2
    Try it without the -w switch so that you have a console where the print writes to. –  Jun 21 '21 at 14:26
  • Leave the `print()` but to know why the app is failing, you have to have a console to which you can see the error code, it might be happening so fast and you may need to capture it with a screen recorder or something – Delrius Euphoria Jun 21 '21 at 14:51
  • @Kemp Did this: `pyinstaller --onefile excludes=['config.py'] project_1.py` is an invalid syntax, doesnt work... – TangerCity Jun 22 '21 at 07:04
  • @CoolCloud could you join https://chat.stackoverflow.com/rooms/232816/discussion – TangerCity Jun 22 '21 at 07:39
  • That goes in your pyinstaller spec file, not on the command line. If you check the [PyInstaller documentation](https://pyinstaller.readthedocs.io/en/stable/usage.html#what-to-bundle-where-to-search) the equivalent argument appears to be `--exclude-module` if you don't want to use a spec file. – Kemp Jun 22 '21 at 08:20
  • @Kemp can you show me the solution regarding my case in code? – TangerCity Jun 22 '21 at 09:31
  • Does this answer your question? [Python ConfigParser.NoSectionError: No section:](https://stackoverflow.com/questions/35016479/python-configparser-nosectionerror-no-section) – Delrius Euphoria Jun 23 '21 at 10:06
  • @CoolCloud, no I always use raw strings. – TangerCity Jun 24 '21 at 10:53

0 Answers0