How to put a Python module in a different folder than my script?

Asked

Viewed 11,584 times

8

I’m trying to run a Python script (v2.4 and v2.7) and created a module with some methods. Unfortunately this module needs to stay in a separate folder.

In the python documentation and so far on Stack Overflow I found numerous post talking to add the __init__.py in every folder of my project.

My problem is that my script is in one structure and my module is in another. My situation is this:

pasta1/
   script.py
pasta2/
   modules/
      python/
          pasta3/
              \__init\__.py
               modulo1.py
               modulo2.py

The part I’m calling these modules in my script is:

#!/usr/bin/python

# -*- coding: utf-8 -*-

import sys  
import datetime  

from pasta3.modulo1 import Mod1  
from pasta3.modulo2 import Mod2 

The mistake:

$ ./script.py   
Traceback (most recent call last):  
     File "./script.py", line 9, in <module> from pasta3.modulo1 import Mod1  
ImportError: No module named pasta3.modulo1

I can’t use the sys.path.append("../modules/python/") because in the Python 2.4 he is not recognized.

How to proceed in this case?

  • Import: No module named pasta3.modulo1, the path is wrong, the file importing is where? This structure is right?

  • Yeah, the structure’s right. In Python 2.7 I was adding sys.path.append("./modules/python/") and it worked, but it was not implemented/works in python 2.4

  • Bred, here we do not use "solved" in the title. Mark an answer as correct or write your own explaining the solution. See How and why to accept an answer?

  • thanks. I had not seen the symbol to accept the answer.

  • puts a doc init.py inside pasture2 in each of the subfolders, maybe it works not sure, otherwise you will have to put the whole directory to be able to access or abbreviate with the ../

4 answers

4

Try to return a directory and indicate the path.

Example:

script py.

from ..pasta3.modulo1 import Mod1
  • I could not make this work in my code. I was able to use this solution: http://answall.com/a/14116/8057

  • @Bred81 the path I put is not right, you should put according to your project, maybe it is from ..pasta2.modules.python.pasta3.modulo1 import Mod1, but if the DBX8 answer solved your problem, mark your answer as accepted. Abs.

  • 1

    I know that the path you have set is not right. But also I could not get it right here...

3


In Python 2.x, you can use the module Imp:

This module provides an interface to the mechanisms used to implement the import statement.

Use the functions find_module and load_module.

  • find_module: Will search the module by name, if the search is successful, the return value is a tuple of 3 elements containing the file, path, and description.

  • load_module: Will import the module, if the module has already been imported, it will only be reloaded - equivalent to function reload. The return is an object that points to the module, otherwise an exception ImportError is launched.

Example:

import imp

arquivo, caminho, descricao = imp.find_module('Modulo1', ['/foo/bar/baz/'])

modulo1 = imp.load_module('Modulo1', arquivo, caminho, descricao)

modulo1.funcao1()

From the version +3.4 python the module Imp became obsolete, as an alternative you can use the module importlib (is available from Python 2.7).

  • Thank you very much! I could only test today. I had to make a few changes to my code and it worked very well!

2

Configure the environment variable PYTHONPATH and include (append if it already exists) the directory pasta3:

export PYTHONPATH = /pasta2/modules/python/pasta3

In any case, you can use the full path to pasta3 instead of relative.

  • This solution until it works, but is not "elegant". Thank you very much!

1

Why not use a symlink for pasta2 within the pasta1:

$ ln -s ./pasta2 ./pasta1/pasta2
  • It was my idea, this one, but the more I grow, the more I have to add symbolic links, which would make this practice ineffective.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.