0

How do I properly write something like this:

from ../lib.model import mymodel

Here is the tree:

lib----->model---->mynodel.py
 |
 |----->myscript--->myscript.py
Kara
  • 6,115
  • 16
  • 50
  • 57
Igor
  • 5,620
  • 11
  • 51
  • 103
  • If you are making a library you might consider using [relative imports](http://stackoverflow.com/questions/11536764/attempted-relative-import-in-non-package-even-with-init-py) – John Sep 19 '13 at 00:53
  • He does in my opinion. But the relative imports are in a sister/brother directory (but still relative). It's not `/lib`, it's a `lib` – Willem Van Onsem Sep 19 '13 at 00:55
  • Where is the script with the `import` statement? – martineau Sep 19 '13 at 02:33

3 Answers3

1

If lib is a package, run myscript as a module and import mymodel like this:

from ..model import mymodel    # relative import

Or:

from lib.model import mymodel    # absolute import

To run myscript.py as a module in the lib package, do one of the following:

  • run a program in the folder containing lib that imports lib.myscript.myscript
  • run myscript.py as a module from the folder containing lib, using python -m lib.myscript.myscript
flornquake
  • 3,156
  • 1
  • 21
  • 32
  • Doesn't this result in quite complex behavior? A programmer should set the proper current directory before executing the program? It solves the problem, but is quite complicated in my opinion. – Willem Van Onsem Sep 19 '13 at 00:51
  • @CommuSoft Normally, the `.py` files inside a package are modules, so they would only be imported and not executed directly. This eliminates the problem you're describing. – flornquake Sep 19 '13 at 00:56
  • Moreover it doesn't work. Using python 2.7 on WinXP SP3 I am getting: future feature absolute_imports is not defined. – Igor Sep 19 '13 at 00:56
  • My bad, it's `absolute_import` without an s. – flornquake Sep 19 '13 at 00:58
  • @flornquake: yeah, but it's more a separation of responsibilities. A script has the responsibility to import the proper modules, not a programmer calling the script or some python program on top of the two... – Willem Van Onsem Sep 19 '13 at 00:58
  • @Igor just realized `absolute_import` isn't needed. – flornquake Sep 19 '13 at 01:28
  • @CommuSoft The only responibility that the programmer has is to treat `lib` as a package and `myscript` as a module if that's what they are. If you do that, the imports within the package will always work out. – flornquake Sep 19 '13 at 01:32
  • @CommuSoft: Generally, modules are installed to a directory in `$PYTHONPATH`, and scripts just use absolute imports. – mipadi Sep 19 '13 at 01:35
  • Don't get me wrong, I gave a +1. There is nothing wrong with your answer. But personally I think the method introduces useless extra responsibilities. Although I have to admit that I don't know Pythons house rules :D. The only argument I can imagine is that `$PYTHONPATH` can be system protected, so you cant install new modules. – Willem Van Onsem Sep 19 '13 at 01:38
1

if your script is using lib you can create a setup.py using file for your project using setuptools

Using setuptools develop command will create a "development mode" version of your project and put it on your python path. It then becomes easy to use it like you would use any python package.

your setup.py can be as simple as:

from setuptools import setup, find_packages


setup(
    name = "lib",
    version = "0.1dev",
    packages = find_packages(),
)

Then you can develop on your project like

python setup.py develop

Now you can import your package into any script you want

from lib.model import model

dm03514
  • 54,664
  • 18
  • 108
  • 145
0

Assuming you call from myscript.py.

Try this:

import sys
sys.path.insert(0, '../model/')
import mynodel

mynodel is probably mymodel, I think you made a typo in your post.

Never put the extension at the imprt statement.

sys.path is a list of paths where python will look for library files. You can simply add a relative path to the directory you want. By putting it at the front of the list, you ensure that python will first look for the file at the specified path (say for instance there is a library with the same name, your file will have priority).

Furthermore it could be useful to give the output of tree (a linux and cmd (Windows) command). This gives standardized output.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555