0

I'm trying to import a file from a different directory, I've tried some of suggestions but none seemed to work for me.

In particular sys.path.insert(...) would not work for me, because I will not have constant absolute path to the file.

I have the following structure where my python files live:

project/packages/package_type/package/package_name/root/usr/bin/

So I need to import

project/packages/package_type/foo/root/usr/bin/foo 
(file does not have .py extension, but is a python file)

from

project/packages/package_type/bar/root/usr/share/bar/modules/foobar/bar.py

The folder with the file I need to import has an empty __init__.py file I also tried

import package-type.foo.root.usr.bin.foo as foo

I am not sure how it's intended to use, since it is intended for Arch Linux package, and the files will simply be exported to /usr/bin

akalikin
  • 1,071
  • 12
  • 35
  • 1
    http://stackoverflow.com/questions/2601047/import-a-python-module-without-the-py-extension (about importing a python file without .py extension) – FunkySayu Jul 20 '15 at 09:36
  • By the way, I did not understand what you really want to do : where are you executing your program ? – FunkySayu Jul 20 '15 at 09:39
  • @FunkySayu It will be arch linux package. So there is a path to it, but I don't want to hardcode it in. – akalikin Jul 20 '15 at 09:40

2 Answers2

0

You could try to use pkg_utils or setuptools.find_packages().

bufh
  • 3,153
  • 31
  • 36
-1

You need to simply provide environment capabilities similar to installed target Arch Linux / whatever package. That is, your Python packages should be reachable through PYTHONPATH. This is more conceptual than technical problem.

As FunkySayu asked in the comment -- where are you executing your program -- this is a crucial question. What is important, your module should be tested somewhere: import'ed and then used. That way you can do with sys.path whatever you want in your test main.py -- in particular sys.path.append(). You should be then able to import any package in a way that will work for installed target package environment.

Konrad Talik
  • 912
  • 8
  • 22