Skip to main content

Timeline for answer to How do I call a function from another .py file? by Nafiul Islam

Current License: CC BY-SA 4.0

Post Revisions

21 events
when toggle format what by license comment
May 3, 2024 at 21:33 comment added Raj Stha You can add an empty file '__init__.py' into each directory and that way when importing the file from different directory you can specify directory name first and then the file name. E.g. from <dir1>.<file1> import <class1/function1> Hope this makes sense.
Aug 30, 2023 at 15:43 comment added JohnnyFun For those coming from a javascript background, note that you don't need quotes around the file name in the import. Also, if it's in a different directory, you can do from myDirectory.someSubDirectory.myFile import yay.
Jul 17, 2022 at 3:55 history edited Mateen Ulhaq CC BY-SA 4.0
Format.
Jan 4, 2022 at 7:28 comment added PatrickT Didn't work with a hyphen in the filename, e.g. from fil-e import *.
Aug 26, 2021 at 16:02 review Suggested edits
Aug 26, 2021 at 18:58
Apr 7, 2021 at 6:21 comment added nuKs @DarkRose Same for me, I fixed it by changing the order of the imports within init.py file, making sure root dependencies were loaded after their transitive dependencies.
Sep 7, 2020 at 11:03 review Suggested edits
Sep 7, 2020 at 11:33
Mar 1, 2018 at 16:50 comment added alex In Python 2 it looks like this either (a) executes the entire script you're referencing with from foo import ... or (b) does not work if foo.py has references to the argparse library.
Sep 16, 2017 at 12:26 history edited Peter Mortensen CC BY-SA 3.0
Active reading.
Jul 5, 2017 at 21:03 comment added quantik Do you have to import all dependencies from file that are used in function as well?
May 14, 2017 at 2:22 comment added Jason newbies try "import file", which should execute only once (singleton)
Sep 27, 2016 at 22:50 comment added Nirvan Sengupta Is there a way to import functions from a.py to a file b.py if they are not in the same directory?
S Sep 20, 2016 at 21:17 history suggested Swanky Coder CC BY-SA 3.0
Typos and grammar corrected. Helps in understanding this answer better
Sep 20, 2016 at 19:04 review Suggested edits
S Sep 20, 2016 at 21:17
Jul 1, 2016 at 2:24 review Suggested edits
Jul 1, 2016 at 5:08
Apr 27, 2016 at 12:20 comment added Nafiul Islam @Tom You have to add that path to the PYTHONPATH variable if it is not already in there.
Apr 27, 2016 at 1:14 comment added Tom @GamesBrainiac, what if the file you want to import functions from is in a different directory? Can I include the filepath preceeding the filename, or is there something more complicated?
Jun 29, 2015 at 7:02 comment added DarkRose I tried this, but it is still showing the error: Has it got anything to do with Python 3, or is a general problem?
Dec 1, 2013 at 7:04 vote accept user2977230
Dec 1, 2013 at 6:37 comment added user2977230 The "file" was just a placeholder for the question I am asking, not the actual file name. Thank you though. I will try this and get back to you.
Dec 1, 2013 at 6:36 history answered Nafiul Islam CC BY-SA 3.0