I have a python file running on /home/app that needs to import a python script from:
/home/app2/
At the top of my file I have:
import app2
sys.path.insert(1, '/home/app2/')
However when running the script I get:
No module named app2
Is there something I am doing wrong? The name of the class I want to import is called {app2} in this case
I am using Python 3.8 on Ubuntu 20.04
Okay I just created the files and folders /home/app/test.py
and /home/app2/test2.py
.
Within /home/app/test.py
I have the following:
def test():
print('working!')
And within /home/app2/test2.py
I have:
from sys import path
path.insert(1, '../app/')
import test
test.test()
Which then prints working!
. So the trick is:
from sys import path
path.insert(1, '../app/') # Can use absolute paths here if desired
When you add /home/app2 to your path, the interpreter is looking there for the module you're trying to import. In the case of your example, it's looking in /home/app2 for...app2. Try this instead:
sys.path.insert(1,'/home')
import app2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.