I’ve the following structure in my simple python project:
JavaScript
x
8
1
MainFolder
2
|
3
├───authentication
4
│ └───apikey.py
5
| └───tokengenerator.py
6
├───Functions
7
│ └───generatedata.py
8
The tokengenerator.py
module produces Token
variables and I need to call it in generatedata.py
module and I used the following code line for this purpose:
JavaScript
1
2
1
from authentication.tokengenerator import Token
2
but it returns the error below:
JavaScript
1
3
1
Exception has occurred: ModuleNotFoundError
2
No module named 'authentication'
3
Would you please advise ?
Advertisement
Answer
from this article, you can add the path below the Functions folder to the searchpath for modules by adding ..
(combined with the scriptpath)
JavaScript
1
10
10
1
import os
2
import sys
3
4
script_dir = os.path.dirname( __file__ )
5
mymodule_dir = os.path.join( script_dir, '..')
6
sys.path.append( mymodule_dir )
7
8
from authentication.tokengenerator import Token
9
token = Token()
10