1

I am trying to run docker containers child1 and child2. lets say we have:

UPDATED

|parent
|-----|child1/
             src_folder/
                       __init__.py
                       mod1.py
|-----|child2/
            __init__.py
            symlink_target_folder
            mody.py
            test1_dir/
                      smfile.py

I have done something like

ln -rs ~/parent/child1/src_folder ~/parent/child2/symlink_target_folder

In mody.py When I do,

from symlink_target_folder import mod1

it works;

but from test1_dir>smfile.py when I do

from .child2.symlink_target_folder import mod1

it throws back ImportError.

I want to know how could I access the same module from that directory ? Could exporting symlink_target_folder to PYTHONPATH someway work it out. I have done

export PYTHONPATH=$PYTHONPATH:/symlink_target_folder

such that ` I can do from mody.py

from symlink_target_folder.mod1 import SmFoo

but I dont think its due to PYTHONPATH. just that symlink_target_folder and mody.py are in same folder.

How do I solve this? What would be a better way to approach this problem? I did check this out

Community
  • 1
  • 1
user2290820
  • 2,709
  • 5
  • 34
  • 62
  • To be sure about what you're asking : the issue is that the script throw an `ImportError` when you're trying to execute `smfile.py` in the directory `test1_dir` with the command `python smfile.py` right ? – FunkySayu Apr 06 '15 at 13:40
  • like `cd child2/test1_dir/ && python smfile.py` right ? – FunkySayu Apr 06 '15 at 13:40
  • @FunkySayu If I have exported the particular dir to pythonpath, say X folder, can I do "From X import *" from a subfolder? in this case that would be test1_dir because it doesnt seem to work; – user2290820 Apr 06 '15 at 16:54
  • @FunkySayu If I have exported the particular dir to pythonpath, say X folder, can I do "From X import *" from a subfolder? in this case that would be test1_dir because it doesnt seem to work; – user2290820 Apr 06 '15 at 16:54
  • 1
    Answer updated. Tested, it work. – FunkySayu Apr 07 '15 at 07:10

2 Answers2

2

So you have the following tree result :

.
├── child1
│   └── srcFolder
│       ├── __init__.py
│       ├── __init__.pyc
│       ├── mod1.py
│       └── mod1.pyc
└── child2
    ├── __init__.py
    ├── mody.py
    ├── symlink_target_folder -> ../child1/srcFolder
    └── test1_dir
        ├── __init__.py
        └── smFile.py

If you put the following headlines in your smFile.py, the importation will work :

import sys
import os

sys.path.append(os.path.abspath(os.sep.join([".."])))

from symlink_target_folder import mod1

mod1.foo()

Instead of using a symbolic link, you can also use that way for adding the path :

sys.path.append(os.path.abspath(os.sep.join(["..", "..", "child1", "srcFolder"])))
FunkySayu
  • 7,641
  • 10
  • 38
  • 61
  • The symlinks is a requirement.i want to know if theres a way to do it with symlinks? – user2290820 Apr 03 '15 at 14:20
  • Did you try `from symlink_target_folder.md1 import SmFoo` ? – FunkySayu Apr 03 '15 at 14:24
  • Thats where Im stuck. I wonder why it throws ImportError: No module named symlink_target_folder.md1; i am running a bash script that exports this symlinked path to PYTHONPATH first. still, it throws error. not sure if its being exported properly. – user2290820 Apr 03 '15 at 14:27
  • I don't understand your issue : I tried with a `cd child2 && ln -s ../child1 child1`, and it works properly. – FunkySayu Apr 03 '15 at 16:30
  • I've updated the question. Could you review it again? – user2290820 Apr 05 '15 at 18:16
  • See the question comments – FunkySayu Apr 06 '15 at 13:41
  • the new edit is same as from ..symlink_target_folder import mod1 right? – user2290820 Apr 07 '15 at 07:13
  • I am also looking for a more universal way thats why ive been searching about exporting to Pythonpath. seems like when i export pythonpath=$pythonpath:/folder_name; i am still not able to do from folder_name import * deep from subfolders.. – user2290820 Apr 07 '15 at 07:14
  • 1
    I really do not recommand to use environnement variables in order to solve your issue. Everything you can do in Python, do it in Python. Also, using the symlinks is not the best thing you can do. By the way, if you want to automatically generate the `sys.path` trick (as you said, a more universal way), checkout `buildout` for Python. Plone, for example, is a big project based on this with multiple modules, and it will automatically generate the start scrips. – FunkySayu Apr 07 '15 at 08:45
  • hm, buildout fits the bill but im looking for a lightweight inbuilt mechanism. I recently got to know about pkgutil and Ive asked a related question here: http://stackoverflow.com/questions/29471039/understanding-how-to-use-pkgutil-what-is-path-in-pkgutil-says-undefined-ke?lq=1 So if I understand how pkgutil works, maybe that will solve all the issue ! – user2290820 Apr 07 '15 at 10:50
1

Here is how I solved this problem;

Given

|parent
|-----|child1/
             src_folder/
                       __init__.py
                       mod1.py
|-----|child2/
            __init__.py
            symlink_target_folder
            mody.py
            test1_dir/
                      smfile.py

i created somepth.pth under child2 file which has this:

symlink_target_folder

Then a simplepth.py under child2 script like this:

import site
import os

site.addsitedir(os.path.dirname(__file__))

When you run child2>simplepth.py it will add the dir where simplepth is and any .pth file which naturally points to /child1/srcFolder and import it to sys.path

now you can simply do:

import mod1

which solves my problem.

so if you only want to run test1_dir/smFile.py you could just add test1_dir 's parent directory and add it to site.addsitedir and then import mod1 and it will work.

user2290820
  • 2,709
  • 5
  • 34
  • 62