I have two folders on my desktop, one called 'testpackage' and another called 'testplay'. 'testpackage' contains an init.py and another file named 'numberstuff.py' (a class with two methods). 'testplay' contains 'play_01.py', a simple script to just check I can add a package to the sys.path without physically adding it into the library in sys.path, I thought I could do this by sys.path.append(path\to\file) on python3 on windows.
testplays 'play_01.py' code:
import sys
for i in sys.path:
print(i,'\n')
sys.path.append('C:\\Users\\priper\\Desktop\\testpackage')
from testpackage import numberstuff
a = numberstuff.maffs()
print(a.sqrtx(3))
console return:
C:\Users\priper\AppData\Local\Programs\Python\Python37\python37.zip
C:\Users\priper\AppData\Local\Programs\Python\Python37\DLLs
C:\Users\priper\AppData\Local\Programs\Python\Python37\Lib
C:\Users\priper\AppData\Local\Programs\Python\Python37
C:\Users\priper\AppData\Local\Programs\Python\Python37\Lib\site-packages
C:\Users\priper\AppData\Roaming\Python\Python37\site-packages
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\
C:\Users\priper\AppData\Local\Programs\Python\Python37\Lib\site-packages\win32
C:\Users\priper\AppData\Local\Programs\Python\Python37\Lib\site-packages\win32\lib
C:\Users\priper\AppData\Local\Programs\Python\Python37\Lib\site-packages\Pythonwin
C:\Users\priper\Desktop\sdnplay
C:\Users\priper\Desktop\mypackage
sdnplay.py
sdnplay.py
sdnplay.py
sdnplay
sdnplay
C:\Users\priper\Desktop\sdnplay
C:\Users\priper\Desktop\sdnplay
C:\Users\priper\Desktop\sdnplay
C:\Users\priper\Desktop\sdnplay
C:\Users\priper\Desktop\testplay
C:\Users\priper\Desktop\testpackage
C:\Users\priper\Desktop\testpackage
Error:
ModuleNotFoundError: No module named 'testpackage'
File"C\Users\priper\Desktop\testplay\play_01.py", line 6, in <module> from testpackage import numberstuff
I can see testpackage is there in the sys.path, I just cannot figure out why it is being treated as a module and why I can't import it>
expected output is '9'.
The only thing I can imagine is that you have called the folder "testpackage" but the file you are trying to import has another name. But since you are trying to import a file named "testpackage" you get the exception.
I have tried to reconstruct your problem but wasn't able to.
my folder/file structur looks as follows:
/full/path/to/the/parent/folder:
- main.py
- theFolder/
- IAMATEST.py
The main.py
import sys
sys.path.append('/full/path/to/the/parent/folder/theFolder')
from IAMATEST import IAMATESTClass
IAMATESTClass.calla()
The IAMTEST.py
class IAMATESTClass:
@staticmethod
def calla():
print("GOT CALLED")
and when executing the main.py I get the output "GOT CALLED".
Good luck!