0

I have been getting different answers on the extent of the import statements in Python. I did some research on Stackoverflow to see what has been said on the topic. So if someone wants to import a python module from the current working directory, a file with a .py extension, then there should be no problem. However if someone wants to import a .py file from a different directory then there are two answers that are conflicting in my view (it maybe because i lack full understanding but I am trying to understand it):

First, this answer `Importing files from different folder in Python' which says that we cannot import files from different folders or directories in python.

Second, this answer `How to import a module given the full path?' which says that we can.

Can someone explain to me what is the difference?

Community
  • 1
  • 1
kolonel
  • 1,412
  • 2
  • 16
  • 33

4 Answers4

3

Roughly speaking, the first example says that you cannot use the import statement to import from a specified location on disk. The second example says that you can achieve a similar effect by using the imp module (which provides access to the internals of the import system). So you can't "really" import such a module (in the sense that you can't use import to do it), but you can use the imp module to "make it work".

So if your question is about the import statement, then no, you can't (without a hack, see below); if your question about just "getting the module loaded" (without necessarily using the import statment), then yes you can. It's sort of like saying "Can you unlock this door? No, because you don't have the key. Can you open this door? Yes, by smashing it in with a sledgehammer."

Also, note that even the first example does provide a way to do it, it's just a bit of an ugly hack.

In practice, the real question is why you feel you need to do this. It is rarely necessary and usually means you're creating a needlessly complicated library structure.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
3

Importing in Python is very simple:

  1. Importing can only be done by referencing modules (not paths)
  2. Python searches for things it can import in the following order:
    1. From the directory where the script was executed.
    2. From the directories in the PYTHONPATH environment variable (if its set).
    3. From the system-wide Python installation's module directory.

It takes these three things and builds the sys.path list and searches it in the order listed. This has some basic problems. For example, if you have a directory with the following files:

foo.py
math.py

In foo.py if you say import math it will import things from math.py and not from the global math library that comes with Python. This is because the current directory is searched first for modules.

To import and use modules from a file system path which is not in the three places that Python searches, you need to add it to the sys.path variable, before you do any imports:

import sys
sys.path.insert(0, '/some/directory/')
from somemodule import something
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
2

Both of the answers are right. The first one uses a straight import statement, and indeed, you can't import from another folder that way.

The first one appends the path to sys.path, which is a list of directories python would search when you import a module. The second one doesn't use an import statement, but using imp.load_source which works differently.

What would you want is usually either to put all of it in the same file, or make a script in a higher folder hierarchy which import all the needed files.

aIKid
  • 26,968
  • 4
  • 39
  • 65
0

Understand this:

When importing a file, Python only searches the current directory, the directory that the entry-point script is running from, and sys.path which includes locations such as the package installation directory

: Quoting from Importing files from different folder in Python
Now lets say you have the files:

I) application/app/folder/file.py
II) application/app2/some_folder/some_file.py

You wish to import the functions from some_file.py to file.py. When you do from application.app.folder.file import func_name, python tries to resolve the path application.app.folder.file. It searches if there is a package/module named application in the current directory which is application/app/folder/ for file.py and the directory paths in sys.path; and python fails to find application in any of the standard search paths. So by default, you can not import paths from some other folder that is not in the python path, but you can tell python about your custom search paths. You do this by adding your paths to the python path(sys.path)

import sys
sys.path.insert(0, 'application/app2/some_folder')

import some_file

If you want to import a file for which you know the absolute/full path (as in How to import a module given the full path?), you do it as

import imp

foo = imp.load_source('module.name', '/path/to/file.py')
foo.MyClass()
Community
  • 1
  • 1
praveen
  • 3,193
  • 2
  • 26
  • 30