简体   繁体   中英

How can I import module if I have an absolute path in Python

I need to import my module foo.py if I've got an absolute path (so module is located in "users/suv/desktop/pr1/pyth" but can be located in any different path as well) . I tried

  import sys

  MYDIR = "users/suv/desktop/pr1/pyth"
  sys.path.append(MYDIR)
  import foo

and I have the following mistake:

Traceback (most recent call last):
  File "./stats", line 7, in <module>
  import foo
ImportError: No module named foo

So what am I doing wrong?

Cheers

# Afile.py
import sys
sys.path.insert(0, '/path/to/application/app/DIRECTORY')

import Afile

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM