Python Forum
Calling functions from another file
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling functions from another file
#1
Hi,

I have two files and I want to call some functions from file A I have done it this way but is not working can someone please tell me what is the issue here?

In file B I have the code below:
from A import ClassA

class B(unittest.TestCase):
   def testA(self):
      ClassA.testA()
Thanks
Reply
#2
where A.py is located? i.e. are you sure Python can find it? Does it have ClassA.testA() method? Can you show us A.py and the full traceback you get?
Reply
#3
File A and B are in separate folders. In File A my code are like this:
class A(unittest.Testcase):
   def testA(self):
       some code...


As you can see in File B I want to call the testA function but this way does not work:
from FileA import A  
 
class B(unittest.TestCase):
   def testA(self):
      A.testA()
Reply
#4
(Oct-11-2017, 11:20 AM)jp2017 Wrote: File A and B are in separate folders.
I would guess that it doesn't work because the import doesn't work - i.e. it cannot find FileA.
read https://docs.python.org/3/tutorial/modul...earch-path

or you should make a package that include FileA
https://docs.python.org/3/tutorial/modul...l#packages

Also nice introduction from Mouse vs. Python
https://www.blog.pythonlibrary.org/2016/...t-imports/
Reply
#5
Importing a python module from a different directory to that which your main module is located in is somewhat fiddly.

Take a look at the stack overflow answer for approaches for different versions of python.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#6
(Oct-11-2017, 11:44 AM)gruntfutuk Wrote: Importing a python module from a different directory to that which your main module is located in is somewhat fiddly.

Take a look at the stack overflow answer for approaches for different versions of python.

Not sure it is good idea for OP to go this road (i.e. interface to the internals of import), as they are not familiar how import itself works
Reply
#7
(Oct-11-2017, 11:53 AM)buran Wrote:
(Oct-11-2017, 11:44 AM)gruntfutuk Wrote: Importing a python module from a different directory to that which your main module is located in is somewhat fiddly.

Take a look at the stack overflow answer for approaches for different versions of python.

Not sure it is good idea for OP to go this road (i.e. interface to the internals of import), as they are not familiar how import itself works

I'm a bit confused at the level setting on here, as earlier I chose not to inflict PEP8 on someone not understanding indentation and someone (you?) felt otherwise, and now I'm being criticised for introducing more complex importing options.

I guess I shall get used to it after a while.

Alternatively, as a beginner at Python, maybe I should just stop trying to help.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#8
(Oct-11-2017, 11:57 AM)gruntfutuk Wrote: I'm a bit confused at the level setting on here, as earlier I chose not to inflict PEP8 on someone not understanding indentation and someone (you?) felt otherwise, and now I'm being criticised for introducing more complex importing options. I guess I shall get used to it after a while. Alternatively, as a beginner at Python, maybe I should just stop trying to help.

Sorry, if I'm misunderstood - in both cases my comments were not meant to criticise you in any way. In both cases I just wanted to make it more clear to the OP. Actually I liked your addition in this thread.
Reply
#9
Thanks for clarifying. I'm trying. Not much on the like front let alone reputation yet, but it's not really a competition. I find I learn quicker if I try to help others.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#10
you can use an __init__.py file at the top level of your app
and an empty on in each directory that has shared code.
To learn how to properly use, watch this (very enjoyable) video by David Beazley:
https://www.youtube.com/watch?v=0oTh1CXRaQ0
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Calling functions by making part of their name with variable crouzilles 4 847 Nov-02-2023, 12:25 PM
Last Post: noisefloor
  How can i combine these two functions so i only open the file once? cubangt 4 869 Aug-14-2023, 05:04 PM
Last Post: snippsat
  Calling a function (which accesses a library) from another file mouse9095 4 832 Jun-07-2023, 08:55 PM
Last Post: deanhystad
  Functions to consider for file renaming and moving around directories cubangt 2 1,766 Jan-07-2022, 02:16 PM
Last Post: cubangt
  Calling functions from within a class: PYQT6 Anon_Brown 4 3,787 Dec-09-2021, 12:40 PM
Last Post: deanhystad
Question trouble with functions "def", calling/defining them Duck_Boom 13 4,402 Oct-21-2020, 03:50 AM
Last Post: Duck_Boom
  Calling C functions with PyObjects jibarra 6 2,737 Jul-17-2019, 02:52 PM
Last Post: jibarra
  Duplicate output when calling a custom function from the same file? road2knowledge 2 2,393 May-10-2019, 07:58 AM
Last Post: road2knowledge
  Use Variables Generated from Functions in different files to use on the main file AykutRobotics 3 2,948 Jan-01-2019, 04:19 PM
Last Post: AykutRobotics
  calling os functions not in module os Skaperen 2 2,637 Nov-10-2018, 01:54 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020