This is a variation of the many questions complaining that one can't import exotic external modules, even though the import works from the terminal. The many answers suggest fixing the execute command in the Build screen. The difference here is that I can't import a module from my own machine, but that it works at both the terminal and Idle. I suspect my problem is a PATH issue. Geany seems like a very nice editor/IDE. But if I can't import even my own modules, it's not very useful.
My simplified directory structure:
py - root for storing python MODULES (apps) and a module of user defined functions (included in PYTHONPATH)
----| main
main.py
----| common (for storing functions accessible by multiple modules
fun.py
The code:
_main.py_
from common import fun
fun.test()_ _
_fun.py_
def test():
print('testing')
_Ouput from Idle_
========================== RESTART: C:/py/test/main.py ========================= testing
_
_
_Output from Geany_
ModuleNotFoundError: No module named 'py'
Original exception was: Traceback (most recent call last): File "test.py", line 1, in <module> from py.common import fun ModuleNotFoundError: No module named 'py'
_ _
_ _
On 2019-12-13 8:53 a.m., paul Marlin wrote:
This is a variation of the many questions complaining that one can't import exotic external modules, even though the import works from the terminal. The many answers suggest fixing the execute command in the Build screen. The difference here is that I can't import a module from my own machine, but that it works at both the terminal and Idle. I suspect my problem is a PATH issue. Geany seems like a very nice editor/IDE. But if I can't import even my own modules, it's not very useful.
My simplified directory structure:
py - root for storing python MODULES (apps) and a module of user defined functions (included in PYTHONPATH)
----| main
main.py
----| common (for storing functions accessible by multiple modules
fun.py
The code:
_main.py_
from common import fun
fun.test()_ _
_fun.py_
def test():
print('testing')
_Ouput from Idle_
========================== RESTART: C:/py/test/main.py
testing
_
_
The path shown `C:/py/test/main.py` isn't how you described your layout, it should be `C:/py/main/main.py` according to how you described it.
_Output from Geany_
ModuleNotFoundError: No module named 'py'
Original exception was: Traceback (most recent call last): File "test.py", line 1, in <module> from py.common import fun ModuleNotFoundError: No module named 'py'
I'm not sure exactly what you're going for, but often you will have your main script in the top level, and then put common/library code in a package directory, with an (often empty) `__init__.py` file. Something like this:
- py - main.py - common - __init__.py - fun.py
If you lay it out like this, it "Just Works" out of the box with Geany's execute command for `main.py`, without messing with any path variables or anything.
If you want to leave it where `main.py` is in a directory that is a sibling of your common/library package, you will probably have to mess with paths and/or use some kind of relative imports.
I don't think your problem is with Geany as much as with trying to understand Python's quite complicated import mechanisms/rules/conventions.
Hope that helps.
Regards, Matthew Brush
On 12/13/19 3:02 PM, Matthew Brush wrote:
I'm not sure exactly what you're going for, but often you will have your main script in the top level, and then put common/library code in a package directory, with an (often empty) `__init__.py` file. Something like this:
- py - main.py - common - __init__.py - fun.py
If you lay it out like this, it "Just Works" out of the box with Geany's execute command for `main.py`, without messing with any path variables or anything.
If you want to leave it where `main.py` is in a directory that is a sibling of your common/library package, you will probably have to mess with paths and/or use some kind of relative imports.
I don't think your problem is with Geany as much as with trying to understand Python's quite complicated import mechanisms/rules/conventions.
Amen
Hope that helps.
Regards, Matthew Brush
============================================
What I'm looking for is a way to structure multiple python applications that all make use of user defined functions (VFP jargon perhaps) stored in a python script (this is how I did it with multiple PHP apps).
I followed your suggestion and took the liberty of adding a second script, main1.py. *Both worked* even without __init__.py. * *
- py - main.py
- main1.py
- common - fun.py
But my problem is that main.py and main1.py really represent separate applications each with multiple files. The only way I know of organizing apps is to place their files in their own directory. So I tried
- py
- app1
- main1.py
- app2
- main2.py
- common - fun.py
Even though this seems to be the same structure as I started with, both main1.py and main2.py worked. So it appears Geany is now accepting /home/paul/py as being in the PYTHONPATH. Since common is downstream of that, it works.
Thank you.
PS: I'm sure this belongs in a separate thread; but I got cocky and tried an alarm clock program that I had written involving a tkinter GUI. It worked fine in Idle; but all I got from Geany was a terminal window telling me the exit code was 0. Using the ancient dicotomy of system vs. applcation programing, wouldn't it be fair to say all application programs are GUI?
Paul
Users mailing list Users@lists.geany.org https://lists.geany.org/cgi-bin/mailman/listinfo/users
I am trying to be helpful about PYTHONPATH, but I don't understand the question very well. This is a complete guess. Ignore as convenient.
It looks to me like Paul wants App1 and App2 to be a root starting-point directories on (or in) the PYTHONPATH and to have another root directory in PYTHONPATH for common stuff, i.e. a library. I think maybe others are recommending that the parent to all those directories be the directory on the root path. Not sure. Not going to go parse the mail either. If I'm way off, just stop reading. :)
If you want the apps to be able to live anywhere with ignorance of each other, then you don't want to use the common parent directory for the PYTHONPATH reference directory? This code snippet might be useful to think about PYTHONPATH management. import logging, os.path, sys
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Add project library home for project use. PARENT_DIR = os.path.dirname(BASE_DIR) LIB_HOME = os.path.join(PARENT_DIR, 'lib') sys.path.append(os.path.abspath(LIB_HOME))
# Now safe to do this, but this is not the only reason for LIB_HOME append: from dmorris.utils.logging import ExceptionFormatter
If you want to check for a directory already in PYTHONPATH, you can test with: if LIB_HOME not in sys.path: sys.path.append( whatever ) And if you are done with a variable, you can remove it, e.g. 'del LIB_HOME'. The lib home will either be located relatively (as above) or absolutely, which is easier to code.
It's been a year since I programmed in Python. I'm rusty. I don't think I ever used __init__.py, which I think is the old way. There was a chapter, guessing chapter 24, in Mark Lutz, Learning Python, 5th Edition. May or may not be dated to irrelevance. It was old when I borrowed it from the library, but it had aged well. I think this was it: Learning Python, 5th Edition
| | | | | |
|
| | | | Learning Python, 5th Edition
Get a comprehensive, in-depth introduction to the core Python language with this hands-on book. Based on author ... |
|
|
The man is painfully thorough. You will know everything you ever wanted to know about PYTHONPATH if you read that part. If not at your public library, there's always interlibrary loan (in the US). There's always the official python documentation at 3.8.1rc1 Documentation
| | | | 3.8.1rc1 Documentation
|
|
|
Dang yahoo replaces by urls. I'm sure there's more documentation out there. Hope this was useful. Pretty inexpensive if not.
Good luck!
Douglas Morris
On Saturday, December 14, 2019, 2:41:07 PM EST, Paul Marlin wurfsendungen@biketrain.net wrote:
On 12/13/19 3:02 PM, Matthew Brush wrote:
I'm not sure exactly what you're going for, but often you will have your main script in the top level, and then put common/library code in a package directory, with an (often empty) `__init__.py` file. Something like this:
- py - main.py - common - __init__.py - fun.py
If you lay it out like this, it "Just Works" out of the box with Geany's execute command for `main.py`, without messing with any path variables or anything.
If you want to leave it where `main.py` is in a directory that is a sibling of your common/library package, you will probably have to mess with paths and/or use some kind of relative imports.
I don't think your problem is with Geany as much as with trying to understand Python's quite complicated import mechanisms/rules/conventions.
Amen
Hope that helps.
Regards, Matthew Brush
============================================
What I'm looking for is a way to structure multiple python applications that all make use of user defined functions (VFP jargon perhaps) stored in a python script (this is how I did it with multiple PHP apps).
I followed your suggestion and took the liberty of adding a second script, main1.py. Both worked even without __init__.py.
- py - main.py
- main1.py
- common - fun.py
But my problem is that main.py and main1.py really represent separate applications each with multiple files. The only way I know of organizing apps is to place their files in their own directory. So I tried
- py
- app1
- main1.py
- app2
- main2.py
- common - fun.py
Even though this seems to be the same structure as I started with, both main1.py and main2.py worked. So it appears Geany is now accepting /home/paul/py as being in the PYTHONPATH. Since common is downstream of that, it works.
Thank you.
PS: I'm sure this belongs in a separate thread; but I got cocky and tried an alarm clock program that I had written involving a tkinter GUI. It worked fine in Idle; but all I got from Geany was a terminal window telling me the exit code was 0. Using the ancient dicotomy of system vs. applcation programing, wouldn't it be fair to say all application programs are GUI?
Paul
_______________________________________________ Users mailing list Users@lists.geany.org https://lists.geany.org/cgi-bin/mailman/listinfo/users
_______________________________________________ Users mailing list Users@lists.geany.org https://lists.geany.org/cgi-bin/mailman/listinfo/users
On 12/14/19 2:40 PM, Paul Marlin wrote:
PS: I'm sure this belongs in a separate thread; but I got cocky and tried an alarm clock program that I had written involving a tkinter GUI. It worked fine in Idle; but all I got from Geany was a terminal window telling me the exit code was 0. Using the ancient dicotomy of system vs. applcation programing, wouldn't it be fair to say all application programs are GUI? ==================================================================
Please ignore this. Idle displays the GUI without the mainloop() function. When I added it, Geany displays the GUI as well.
the main loop of a GUI tries to act like an internet browser: disguise as OS and do as you like; it's actually the bottom line of 'social networks' (or even wider, humans simply act like that: finding out without leaving the cover); I just wonder how long they can sell there 'data analysis' (AI, there selling label, is older than anyone's live on this planet but still working fine); don't fight, be amused.. have a nice day
-----Original Message----- From: Paul Marlin Sent: Saturday, December 14, 2019 11:55 PM To: users@lists.geany.org Subject: Re: [Geany-Users] Can't import local module. Need mainloop()
On 12/14/19 2:40 PM, Paul Marlin wrote:
PS: I'm sure this belongs in a separate thread; but I got cocky and tried an alarm clock program that I had written involving a tkinter GUI. It worked fine in Idle; but all I got from Geany was a terminal window telling me the exit code was 0. Using the ancient dicotomy of system vs. applcation programing, wouldn't it be fair to say all application programs are GUI? ==================================================================
Please ignore this. Idle displays the GUI without the mainloop() function. When I added it, Geany displays the GUI as well.
_______________________________________________ Users mailing list Users@lists.geany.org https://lists.geany.org/cgi-bin/mailman/listinfo/users
How are you starting Geany? Its possible you are starting it in a way that PYTHONPATH is not set. Perhaps test it by running a small Python program with Geany to print sys.path.
Cheers Lex
On Sat, 14 Dec 2019 at 02:54, paul Marlin wurfsendungen@biketrain.net wrote:
This is a variation of the many questions complaining that one can't import exotic external modules, even though the import works from the terminal. The many answers suggest fixing the execute command in the Build screen. The difference here is that I can't import a module from my own machine, but that it works at both the terminal and Idle. I suspect my problem is a PATH issue. Geany seems like a very nice editor/IDE. But if I can't import even my own modules, it's not very useful.
My simplified directory structure:
py - root for storing python MODULES (apps) and a module of user defined functions (included in PYTHONPATH)
----| main
main.py
----| common (for storing functions accessible by multiple modules
fun.py
The code:
main.py
from common import fun
fun.test()
fun.py
def test():
print('testing')
Ouput from Idle
========================== RESTART: C:/py/test/main.py ========================= testing
Output from Geany
ModuleNotFoundError: No module named 'py'
Original exception was: Traceback (most recent call last): File "test.py", line 1, in <module> from py.common import fun ModuleNotFoundError: No module named 'py'
Users mailing list Users@lists.geany.org https://lists.geany.org/cgi-bin/mailman/listinfo/users
an whilst you are at it set up a virtual environment (venv) to keep the versions nicely apart (batteries are included); in Geany, each environment corresponds to a particular project (with its respective settings)..
-----Original Message----- From: Lex Trotman Sent: Friday, December 13, 2019 10:22 PM To: Geany general discussion list Subject: Re: [Geany-Users] Can't import local module
How are you starting Geany? Its possible you are starting it in a way that PYTHONPATH is not set. Perhaps test it by running a small Python program with Geany to print sys.path.
Cheers Lex
On Sat, 14 Dec 2019 at 02:54, paul Marlin wurfsendungen@biketrain.net wrote:
This is a variation of the many questions complaining that one can't import exotic external modules, even though the import works from the terminal. The many answers suggest fixing the execute command in the Build screen. The difference here is that I can't import a module from my own machine, but that it works at both the terminal and Idle. I suspect my problem is a PATH issue. Geany seems like a very nice editor/IDE. But if I can't import even my own modules, it's not very useful.
My simplified directory structure:
py - root for storing python MODULES (apps) and a module of user defined functions (included in PYTHONPATH)
----| main
main.py
----| common (for storing functions accessible by multiple modules
fun.py
The code:
main.py
from common import fun
fun.test()
fun.py
def test():
print('testing')
Ouput from Idle
========================== RESTART: C:/py/test/main.py
testing
Output from Geany
ModuleNotFoundError: No module named 'py'
Original exception was: Traceback (most recent call last): File "test.py", line 1, in <module> from py.common import fun ModuleNotFoundError: No module named 'py'
Users mailing list Users@lists.geany.org https://lists.geany.org/cgi-bin/mailman/listinfo/users
_______________________________________________ Users mailing list Users@lists.geany.org https://lists.geany.org/cgi-bin/mailman/listinfo/users
I started Geany from the Ubunto menu.
I ran:
*import sys*
*print(sys.path)*
First I tried Geany and got
*/tmp/geany_run_script_UR8SC0.sh: 7: ./sys: Permission denied** **------------------** **(program exited with code: 126)*
Then I tried Idle and got
*['/home/paul/py/test', '/home/paul', '/usr/bin', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', '/home/paul/.local/lib/python3.7/site-packages', '/usr/local/lib/python3.7/dist-packages', '/usr/lib/python3/dist-packages']*
From the terminal I got
*['', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', '/home/paul/.local/lib/python3.7/site-packages', '/usr/local/lib/python3.7/dist-packages', '/usr/lib/python3/dist-packages']*
I think I see the problem. Python is looking in * * * * */home/paul/.local/lib/python3.7/site-packages* * * but I was sure I added ** ** **/home/paul/py** ** ** to PYTHONPATH
How can I change the path? An a rhetorical question. Why is Idle smarter?
Thank you for your response.
Paul **======================================================================= ** ** ** On 12/14/19 8:28 AM, Georg Klingenberg wrote:
an whilst you are at it set up a virtual environment (venv) to keep the versions nicely apart (batteries are included); in Geany, each environment corresponds to a particular project (with its respective settings)..
-----Original Message----- From: Lex Trotman Sent: Friday, December 13, 2019 10:22 PM To: Geany general discussion list Subject: Re: [Geany-Users] Can't import local module
How are you starting Geany? Its possible you are starting it in a way that PYTHONPATH is not set. Perhaps test it by running a small Python program with Geany to print sys.path.
Cheers Lex
On Sat, 14 Dec 2019 at 02:54, paul Marlin wurfsendungen@biketrain.net wrote:
This is a variation of the many questions complaining that one can't import exotic external modules, even though the import works from the terminal. The many answers suggest fixing the execute command in the Build screen. The difference here is that I can't import a module from my own machine, but that it works at both the terminal and Idle. I suspect my problem is a PATH issue. Geany seems like a very nice editor/IDE. But if I can't import even my own modules, it's not very useful.
My simplified directory structure:
py - root for storing python MODULES (apps) and a module of user defined functions (included in PYTHONPATH)
----| main
main.py
----| common (for storing functions accessible by multiple modules
fun.py
The code:
main.py
from common import fun
fun.test()
fun.py
def test():
print('testing')
Ouput from Idle
========================== RESTART: C:/py/test/main.py
testing
Output from Geany
ModuleNotFoundError: No module named 'py'
Original exception was: Traceback (most recent call last): File "test.py", line 1, in <module> from py.common import fun ModuleNotFoundError: No module named 'py'
Users mailing list Users@lists.geany.org https://lists.geany.org/cgi-bin/mailman/listinfo/users
Users mailing list Users@lists.geany.org https://lists.geany.org/cgi-bin/mailman/listinfo/users _______________________________________________ Users mailing list Users@lists.geany.org https://lists.geany.org/cgi-bin/mailman/listinfo/users
On Sun, 15 Dec 2019 at 00:36, Paul Marlin wurfsendungen@biketrain.net wrote:
I started Geany from the Ubunto menu.
I suspected so, (most) desktops start the application directly without running a shell. But the shell is the thing that reads your .bashrc or .profile or wherever you set PYTHONPATH. So without the shell being run first, geany is started with an environment that does not include PYTHONPATH.
You can confirm this by starting Geany from the terminal not the menu.
The likely solution is to make the menu run a shell script that runs geany, and maybe thats how idle is launched. Also some desktop menu editors have an option to run the command in a shell built in, I'm not a Ubuntist so I don't know what it has, probably check first and make a start script if needed.
Cheers Lex
I ran:
import sys
print(sys.path)
First I tried Geany and got
/tmp/geany_run_script_UR8SC0.sh: 7: ./sys: Permission denied
(program exited with code: 126)
Then I tried Idle and got
['/home/paul/py/test', '/home/paul', '/usr/bin', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', '/home/paul/.local/lib/python3.7/site-packages', '/usr/local/lib/python3.7/dist-packages', '/usr/lib/python3/dist-packages']
From the terminal I got
['', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', '/home/paul/.local/lib/python3.7/site-packages', '/usr/local/lib/python3.7/dist-packages', '/usr/lib/python3/dist-packages']
I think I see the problem. Python is looking in
/home/paul/.local/lib/python3.7/site-packages
but I was sure I added
/home/paul/py
to PYTHONPATH
How can I change the path? An a rhetorical question. Why is Idle smarter?
Thank you for your response.
Paul
On 12/14/19 8:28 AM, Georg Klingenberg wrote:
an whilst you are at it set up a virtual environment (venv) to keep the versions nicely apart (batteries are included); in Geany, each environment corresponds to a particular project (with its respective settings)..
-----Original Message----- From: Lex Trotman Sent: Friday, December 13, 2019 10:22 PM To: Geany general discussion list Subject: Re: [Geany-Users] Can't import local module
How are you starting Geany? Its possible you are starting it in a way that PYTHONPATH is not set. Perhaps test it by running a small Python program with Geany to print sys.path.
Cheers Lex
On Sat, 14 Dec 2019 at 02:54, paul Marlin wurfsendungen@biketrain.net wrote:
This is a variation of the many questions complaining that one can't import exotic external modules, even though the import works from the terminal. The many answers suggest fixing the execute command in the Build screen. The difference here is that I can't import a module from my own machine, but that it works at both the terminal and Idle. I suspect my problem is a PATH issue. Geany seems like a very nice editor/IDE. But if I can't import even my own modules, it's not very useful.
My simplified directory structure:
py - root for storing python MODULES (apps) and a module of user defined functions (included in PYTHONPATH)
----| main
main.py
----| common (for storing functions accessible by multiple modules
fun.py
The code:
main.py
from common import fun
fun.test()
fun.py
def test():
print('testing')
Ouput from Idle
========================== RESTART: C:/py/test/main.py ========================= testing
Output from Geany
ModuleNotFoundError: No module named 'py'
Original exception was: Traceback (most recent call last): File "test.py", line 1, in <module> from py.common import fun ModuleNotFoundError: No module named 'py'
Users mailing list Users@lists.geany.org https://lists.geany.org/cgi-bin/mailman/listinfo/users
Users mailing list Users@lists.geany.org https://lists.geany.org/cgi-bin/mailman/listinfo/users _______________________________________________ Users mailing list Users@lists.geany.org https://lists.geany.org/cgi-bin/mailman/listinfo/users
Users mailing list Users@lists.geany.org https://lists.geany.org/cgi-bin/mailman/listinfo/users