Aug-28-2019, 04:05 PM
Sorry: I'm not yet used to this forum, I managed to file an incomplete question. Here is the completed question:
I use Win10 and Python 3.7.4. I am rather new to Python.
I wrote a python program that contains a number of inline functions and classes followed by a main program. It imports some “standard” modules (sys, decimal, tkinter, …) and also a home-grown module that contains functions and classes that are also used in other modules.
My modules look (simplified) like:
main.py
import sys
from selfwrittenmodule import f1, f2
def if1(x):
….a = f1(x);return(a)
def if2(x):
….a = f2(x);return(a)
b = 2
a = if1(b)
c = if2(b)
print (a, c)
selfwrittenmodule.py
def f1(x):
….some lines of code
def f2(x)
….some lines of code
I stored the “main.py” module in a folder dedicated to “work in progress”. Because “selfwrittenmodule,py” is for use by many other scripts to, it is stored in a folder dedicated to “general use” modules.
In the manuals I found three ways to ensure that the python interpreter finds modules in other folders:
1) Add a “PYTHONPATH” variable to the system variables and have it point to the “general use” folder.
2) Add the “general use” folder to the “%PATH%” system variable.
3) Move the “general use” folder, with the “selfwrittenmodule.py” in it, into C:\Users\actname\AppData\Local\Programs\Python\Python37-32\Lib
I tried all three methods, no joy: when I press “F5” in idlex, I receive an error message that the “selfwrittenmodule.py” is not found.
Then I moved “selfwrittenmodule.py” to the folder that contains “main.py” and press f5 in idlex. Now the python interpreter finds “selfwrittenmodule.py” and “main” is “compiled” and executed fine.
Question: how can I store “selfwrittenmodule.py” in a folder different from my “work in progress” folder and be able to import it correctly in another module by the python interpreter?
I use Win10 and Python 3.7.4. I am rather new to Python.
I wrote a python program that contains a number of inline functions and classes followed by a main program. It imports some “standard” modules (sys, decimal, tkinter, …) and also a home-grown module that contains functions and classes that are also used in other modules.
My modules look (simplified) like:
main.py
import sys
from selfwrittenmodule import f1, f2
def if1(x):
….a = f1(x);return(a)
def if2(x):
….a = f2(x);return(a)
b = 2
a = if1(b)
c = if2(b)
print (a, c)
selfwrittenmodule.py
def f1(x):
….some lines of code
def f2(x)
….some lines of code
I stored the “main.py” module in a folder dedicated to “work in progress”. Because “selfwrittenmodule,py” is for use by many other scripts to, it is stored in a folder dedicated to “general use” modules.
In the manuals I found three ways to ensure that the python interpreter finds modules in other folders:
1) Add a “PYTHONPATH” variable to the system variables and have it point to the “general use” folder.
2) Add the “general use” folder to the “%PATH%” system variable.
3) Move the “general use” folder, with the “selfwrittenmodule.py” in it, into C:\Users\actname\AppData\Local\Programs\Python\Python37-32\Lib
I tried all three methods, no joy: when I press “F5” in idlex, I receive an error message that the “selfwrittenmodule.py” is not found.
Then I moved “selfwrittenmodule.py” to the folder that contains “main.py” and press f5 in idlex. Now the python interpreter finds “selfwrittenmodule.py” and “main” is “compiled” and executed fine.
Question: how can I store “selfwrittenmodule.py” in a folder different from my “work in progress” folder and be able to import it correctly in another module by the python interpreter?