0

i need to call a function from from one python class to another which are at different directories. I'm using Eclipse and PyDev for developing scripts.

sample.py

class Employee:
 def meth1(self,arg):
        self.arg=arg
        print(arg)

ob=Employee()
ob.meth1("world")

main.py

class main:
    def meth(self,arg):
        self.arg=arg
        print(arg)


obj1=main()
obj1.meth("hello")

I need to access meth1 in main.py

updated code

main.py
from samp.sample import Employee
class main:
    def meth(self,arg):
        self.arg=arg
        print(arg)


obj1=main()


obj1.meth("hello")

After executing main.py it is printing "world" automatically without calling it.

my requirement is i need to call meth1 from main.py explicitly

please find my folder below Eclipse folder hierarchy

Sid
  • 645
  • 1
  • 7
  • 19

3 Answers3

0

import is the concept you need here. main.py will need to import sample and then access symbols defined in that module such as sample.Employee

To ensure that sample.py can be found at import time, the path to its parent directory can be appended to sys.path (to get access to that, of course, you will first need to import sys). To manipulate paths (for example, to turn a relative path like '../samp' into an absolute path, you might want to import os as well and take a look at the standard library functions the sub-module os.path has to offer.

jez
  • 14,867
  • 5
  • 37
  • 64
  • just as you said i added import statement in main.py and "__init__ in each directory" **from samp.sample import ob** when i executed main.py the output is "world" "hello" i mean "world" is displayed automatically – Sid Dec 24 '14 at 05:34
  • @Sid the first time you `import` a file, the code in it will be executed, like your `ob=Employee()` etc. if you want to defer execution of those lines, tidy them away into a function which you'll then call only when needed. – jez Dec 24 '14 at 13:39
-1

You will have to import the sample.py file as a module to your main.py file. Since the files are in different directories, you will need to use the __init__.py

From the documentation:

The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later.

Here is a related stack overflow question which explains the solution in more detail.

Community
  • 1
  • 1
Jacob Bridges
  • 725
  • 2
  • 8
  • 16
-1

It's generally not a good idea to alter the sys.path variable. This can make scripts non-portable. Better is to just place your extra modules in the user site directory. You can find out what that is with the following script.

import os
import sys
import site

print(os.path.join(site.USER_BASE, "lib", "python{}.{}".format(*sys.version_info[0:2]), "site-packages"))

Place your modules there. Then you can just import as any other module.

import sample

emp = sample.Employee()

Later you can package it using distutils or setuptools and the imports will still work the same.

Keith
  • 42,110
  • 11
  • 57
  • 76