0

I've succeeding in importing a single file, but the file calls other files and I get an error. So I'm trying to import and entire folder. I would prefer not to import each file one by one since I know it must be possible to import the whole folder. Here is the syntax I used to import a file:

import importlib.machinery import os temp_directory2 = '/Users/me/PycharmProjects/inference_engine2/inference2/ancient/temp.py' temp_directory = '/Users/me/PycharmProjects/inference_engine2/inference2/Proofs/main_loop.py' main_directory = '/Users/me/PycharmProjects/inference_engine2/inference2/Proofs/' b = os.path.exists(temp_directory) loader = importlib.machinery.SourceFileLoader('temp', temp_directory) handle = loader.load_module('temp') 
1
  • "but the file calls other files and I get an error" – how does that file "call other files"?! Each module should declare and import its own dependencies, it shouldn't depend on some other module loading its dependencies. Commented Jan 24, 2018 at 16:26

2 Answers 2

2

You can add the path to the list sys.path at the beginning of your file, like so:

import sys; sys.path.insert(0, r'C:/Users/me/PycharmProjects/inference_engine2/inference2/Proofs') 

Note since you are inserting the path at the beginning of the list, this is the first place python will go to look for a module.

Sign up to request clarification or add additional context in comments.

Comments

1

Convert it to a package using __init__.py. more here : https://docs.python.org/2/tutorial/modules.html

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.