0

I have a module located in a different directory. I want to import it in another module. When I try to import from the directory, I get a syntax error. How do I import other local modules?

from "C:\User\Sam\module" import abc 
davidism
  • 121,510
  • 29
  • 395
  • 339
Sam
  • 11

1 Answers1

1

Ideally, you should build and install packages the standard way. Using a virtualenv and pip will set up your path for you, and install/symlink modules in a standard location. If for some reason you don't want to do that, see below.


If the module is not in the working directory you started Python from, add the folder containing your module to the path, then import the module by name.

import sys
sys.path.insert(0'C:/User/Sam/local_modules')
import my_module

Modifying the path is never necessary if you follow the standard package guidelines.

davidism
  • 121,510
  • 29
  • 395
  • 339