0

Lets say we have two programs A.py and B.py ,now B.py has two defined functions

calculator(x,y) which returns int and makelist(list1)which returnslist`

Now,how can I access these functions in A.py (Python 3)?

skvatss
  • 35
  • 1
  • 1
  • 8
  • 1
    I think this post is similar to the one [here](http://stackoverflow.com/questions/714881/how-to-include-external-python-code-to-use-in-other-files). I hope that helps – aakashgupta.0205 Jun 22 '16 at 05:20
  • @aakashgupta.0205 i imported all functions using command `from B import* ' now it is showing import error that no module named A ,even when i saved both the programs in same location i also changed A to A.py then also same error. – skvatss Jun 22 '16 at 05:50
  • you're importing B into A, am i right? – aakashgupta.0205 Jun 22 '16 at 06:02

1 Answers1

0

You will need to import the other file, that is B, as a module

import B

However, this will require you to prefix your functions with the module name. If instead, you want to just import specific function(s) and use it as it is, you can

from B import *    # imports all functions from B

-or-

from B import calculator   # imports only the calculator function from B

UPDATE

Python does not add the current directory to sys.path, but rather the directory that the script is in. So, you would be required to add your directory to either sys.path or $PYTHONPATH.

aakashgupta.0205
  • 647
  • 1
  • 8
  • 23