-2

I am posting this because the answers to this question (Import a module from a relative path) are not usable for those of us likely to be asking the question in the first place.

Suppose I have the following file structure (I have kept the same naming convention from the other question):

C:\dirMain\
     dirFoo\
        Foo.py
     dirBar\
        Bar.py

I want to import Bar.py from within Foo.py.

Something like this: (My C is showing here, sorry):

# Foo.py
from ../dirBar/Bar import *

Please feel free to mark as a duplicate but do check the other responses first; most of the ones I have seen are overly complicated, don't work, or are incomplete. This is a simple question for which there is hopefully a simple answer.

Things I've tried:

1) The suggestion below from Puffin GDI:

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

result:

NameError: name 'file' is not defined

The solution to this ostensibly lies amidst this answer, where I do not know.

2) This from here: How to import a module given the full path?

import imp
abc = imp.load_source('bar.py', 'C:\dirMain\dirBar.py')

result:

IOError: [Errno 22] Invalid Argument

3) As discussed here: Import a module from a relative path

(first added __init__.py to /dirBar)

from ..dirBar import Bar

result:

ValueError: Attempted relative import in non-package

Community
  • 1
  • 1
quant
  • 21,507
  • 32
  • 115
  • 211
  • 1
    Look back over those duplicates. The answer is, basically, you can't directly import relative to the directory structure. You can only import relatively *within packages*, which means you must add `__init__.py` files to each subdirectory to make them packages. – BrenBarn Oct 29 '13 at 05:43
  • @BrenBarn, I've tried creating that file, but then what? – quant Oct 29 '13 at 05:44
  • Then you do `from ..dirBar import Bar`, as discussed in all those duplicate questions. You must also have `__init__.py` in `dirMain` and the directory containing `dirMain` must be on the Python library path, as also discussed in all those duplicate questions. – BrenBarn Oct 29 '13 at 05:45
  • @BrenBarn I just get the error "ValueError: Attempted relative import in non-package" – quant Oct 29 '13 at 05:46
  • 3
    @ausainman: See [this question](http://stackoverflow.com/questions/11536764/attempted-relative-import-in-non-package-even-with-init-py/11537218#11537218) and [this one](http://stackoverflow.com/questions/14132789/python-relative-imports-for-the-billionth-time/14132912#14132912), just two among many of the zillion duplicates of which you are already aware. – BrenBarn Oct 29 '13 at 05:48
  • @BrenBarn well I have read those but they are either a) solutions that don't work b) essays on what a module is or c) too complicated for me to understand. I have been writing C++ for years so I like to think I'm capable of comprehending this stuff, but I seem to be mistaken! – quant Oct 29 '13 at 05:51
  • @BrenBarn maybe something useful for me to know would be what exactly a python programmer does when he has functions used by files in different folders. Does he make lots of copies? – quant Oct 29 '13 at 05:52
  • 1
    That is maybe a good question to ask on its own. What I'd say is, if `Foo` needs `Bar` because they're both part of one project, make them all part of a package. If they're independent libraries, make them actual libraries in directories on the Python path. The basic thing about Python libs is everything works based on the Python path (`sys.path`), so you're better off putting things in directories on the path --- that is, make your libraries real libraries that are globally available. What becomes painful is if you want to import between arbitrary directories that are not on the path. – BrenBarn Oct 29 '13 at 05:55

1 Answers1

0

Import your root path in your all programs. Start travel path from the root in all programs.

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

or

root = "C:\dirMain"
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), root)))

You can write root path in your configure, then

from dirBar.Bar import class_name
Puffin GDI
  • 1,702
  • 5
  • 27
  • 37