[pyar] Importar desde carpetas paralelas.

Javier Marcon javiermarcon en gmail.com
Mar Oct 6 10:13:24 ART 2015


El 06/10/15 a las 09:52, Alejandro Caro escribió:
> Tengo esta estructura:
>
> Directorio1
> -- __init__.py
>
> --DirectorioA
> -- -- __init__.py
> -- -- moduloA.py
>
> --DirectorioB
> -- --__init__.py
> -- --moduloB.py
>
>
> Parado en Directorio1 hago from DirectorioA.moduloA import * y anda
> perfecto.
> *Parado en* *DirectorioB* hago from Directorio1.DirectorioA.moduloA
> import * y naranja, me tira la excepción de que no puede importar el
> módulo.
>
> No quiero agregarle un path a sys, lo que me gustaría saber es como
> acceder gracias a los __ini__.py a directorios paralelos en Python 2.7.3
>
>
>
> Alejandro Caro.
>
>
Podes usar imp con paths relativos ademas de con absolutos:

javier en javier-System-Product-Name:~/prueba$ ls -l modulo*
modulo1:
total 0
-rw-rw-r-- 1 javier javier 0 oct  6 10:00 __init__.py
-rw-rw-r-- 1 javier javier 0 oct  6 10:00 prog1.py

modulo2:
total 0
-rw-rw-r-- 1 javier javier 0 oct  6 10:00 __init__.py
-rw-rw-r-- 1 javier javier 0 oct  6 10:00 prog2.py
javier en javier-System-Product-Name:~/prueba$ cd modulo1
javier en javier-System-Product-Name:~/prueba/modulo1$ python
Python 2.7.9 (default, Apr  2 2015, 15:33:21)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import imp
>>> foo = imp.load_source('modulo2', '../modulo2/prog2.py')
>>> quit()
javier en javier-System-Product-Name:~/prueba/modulo1$

Saludos,

Javier.

------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: <http://listas.python.org.ar/pipermail/pyar/attachments/20151006/7f529d06/attachment.html>


More information about the pyar mailing list