-1

So I routinely call the same 15-20 import statements prior to running multiple, but different .py scripts. This can sort of look clunky and I was wondering if I could, for the sake of condensing, store all of these import statements in a separate .py file, then call those import statements using a 1 - liner at the beginning of these .py scripts.

e.g.:


import a
import b
import c
import d 
import e
import f

(and so on so forth)

into:

import import_list import imports

I've tried search around but I'm not sure I'm using the correct language to get my answer. I'm sure this has been asked quite a few times though. can anybody help? thanks!

Nate
  • 136
  • 10

1 Answers1

1

If your import list is constant, you could do something like creating a separate python file like

import_list.py

import a
import b
import c
import d 
import e
import f

Then add 1 liner to your files:

 from import_list import *
user2827262
  • 157
  • 8
  • 1
    this is your example is in the same directory as the .py file in hand, right? what if i need to do the same thing, but the import_list.py is in a different directory? (the files have to be taxonomized in a particular way). sorry if this is obvious, i'm new :( – Nate Oct 02 '20 at 15:08
  • 1
    hi, if you are within the same package, for upper directory you can `from ..import_list import *`, if it's in a subdirectory, you can do `from sub_dir.import_list import *`, if neither you can extend your system path with the directory that import_list is located in. For instance if the directory is one up, but not within the package: `import sys sys.path.insert(1,'..') from import_list import *` or the full path instead of ` .. ` . check this out: https://stackoverflow.com/questions/62886769/is-it-possible-to-import-a-python-file-from-outside-the-directory-the-main-file – user2827262 Oct 02 '20 at 15:29