My program is not finding another file

The following code has a problem:

#! /usr/bin/env python3
#  -*- coding: utf-8 -*-
#
# GUI module generated by PAGE version 8.0
#  in conjunction with Tcl version 8.6
#    May 11, 2024 01:17:29 PM BST  platform: Darwin

import sys
import tkinter as tk
import tkinter.ttk as ttk
from tkinter.constants import *
import os.path

_location = os.path.dirname(__file__)

import LoginForm_support

The last line is the problem. I get an error message “Path recognition problem. import LoginForm_support could not be resolved.” This is a python file with the balance of the coding.
I am using Visual Studio Code on a Mac mini M1 computer.
My directory setup from Finder is:
/neilcampbell/PageProjects/LoginForm plus the python files
I used Page to generate the python files in the above directory.
As this program is only going to be used by me on my computer, should I just hard code the path? If so would I replace the path line above with:
_location = os.path.dirname(/neilcampbell/PageProjects/LoginForm/LoginForm_support)?
Any help would be appreciated.

Hi,

Is the module LoginForm_support a file that you created, an external library? Can you elaborate further please.

Hi Paul

Thanks for your prompt reply.

The file LoginForm_support(.py) is my python file with functions that need to be called by the main python file LoginForm.py from which the above code was taken. Sorry, I should have mentioned that because as a fellow py file it won’t need installing.
Any further thoughts? Thanks.

As a quick test, can you please move that file to the same location (folder) as your main file? Because when two files exist in the same folder, they can import from each other without any PATH issues. So, if this quick test resolves the issue, then it is a PATH definition issue in your original post.

hi Neil,
you need to check the documentation of the Page tool you’re using, in fact there is a tutorial under the folder:
page/docs/tutorials/Learning Page/Learning PAGE - A Python GUI Designer Tutorial v7.0b.pdf

At page 19 it’ll guide you to create the ‘support module’ for your GUI, which is actually the file you’re missing:

Back in the Main Page window, click again on Gen_Python | Generate Support Module…

1 Like

Thanks Paul. Both files are in the same folder.

Hi Amedeo
Thanks for your reply. I actually used the tutorial to create the Login program. I created both LoginForm.py and LoginForm_support.py by Generating the files from the Page app and both files are in the same folder. However I can’t get these programs to Run using Visual Studio Code. The Run stops at the last line in the clip I posted above.

Hi Amedeo

I was looking further in that tutorial. On page 26 under What You Have Leanred, it says:
How to properly start PAGE.
Always remember, start PAGE from the development folder for your project.
I don’t understnad how to do this. Page has its own folder. How can it be started in the development project folder? Unless I copy the page.py file into the development folder, but, without the other page support folders, i don’t see how this can happen.
Any ideas, because this must have somewthing to do with pegging the location of the generated files.
Thanks for your time.

well I’ve never used Page before but I think it means to run Page from the folder where the project is.
I’m starting Page from the terminal (you can do it also from the VSCode terminal) making sure I’m on the project folder (which is the default when starting the VSCode terminal) then I launch

/....complete-path-to-page.../page

regarding running the project from VSCode, I had no problem, I just opened the folder from VSCode, opened the file and then run it from the Run menu:

image

Whenever any program starts up on your computer - including a Python interpreter, which VSC will start up for you when you use the Run option - it starts “in” a folder called the current working directory.

When you use the command line to start a program, it’s easy to know and control which folder this is: it’s the one that you see in the prompt when you type the python command, and you use the cd command to move through the directory tree (cd .. goes up a level, and cd folder goes into the folder subfolder).

When you let an IDE start up Python for you, you need to understand how the IDE chooses a current working directory. Usually this will be in the settings somewhere, and explained in the documentation for the IDE.

It sounds like PAGE is another program: a tool which generates this Python code for you and also can make use of it. I don’t know anything specific about VSC, so I can’t tell you if it knows anything (or can be told anything) about external programs like that. But if you want to run PAGE from the command line, it’s just like @amedeo described. And, yes, VSC does provide a terminal (i.e., command line) window for you which should work the same way as the cmd one.

In general, if another program wrote code for you (in Python or any other language), you should not directly edit that code unless the program’s documentation explicitly tells you to do so. Instead, you should change something in the source material that the program uses to generate the code.

In this case, simply changing _location would not help anyway. That’s just a value that the rest of the code is presumably using, so that it can - for example - locate resource files, relative to the generated code. But it can’t fix the import problem, because from Python’s perspective it did nothing relevant to the import system. In fact, _location would already have the name of the folder where the Python code you showed is (and, therefore, where you want Python to look for import LoginForm_support. But Python does not necessarily look for imports in the same folder where the current source file is.


Now that I’ve given the appropriate warnings and guidance, you are clear to learn the underlying material :wink:

And specifically my answer here, which I’ve directly linked:

1 Like

Thank you very much Amedeo. I will look at this in the morning.

Karl. You have really gone the extra mile with all the information you have given me. I will look at all the Python etc links you gave me in order to understand what I am doing. I am not a programmer - I programmed in VB on a PC many many years ago, but doing it on a Mac is a whole new experience. Thank you. I will be in touch again, either way.

I have spent some time reading the input from your links and have learnt a lot. I hear you about the path and I looked deeper into my problem. It seems that there were syntax errors in the file to be imported and this was part of the reason that it wouldn’t import it. I have corrected these. I also changed my program line 14 from:
_location = os.path.dirname(file)
to
os.chdir(“/users/neilcampbell/PageProjects/Loginform”)
and the program now runs properly.
Thank you so (and @amedeo) so very much for the time you have taken to help me.

Happy that it worked at the end!