Python Forum
importing functions from a separate python file in a separate directory
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
importing functions from a separate python file in a separate directory
#1
So I can easily call a function from a file in the same directory as the one i'm running:
from functestsetup import functest
functest()
but it gets tricky when i attempt to do it from a different directory...
Error:
Traceback (most recent call last): File "O:\PyMemer BETA\functest.py", line 1, in <module> from functestsetup import functest ModuleNotFoundError: No module named 'functestsetup'
what I intend to do is have a subfolder of functions for the program to call upon, making the code way more efficient. Anyone have any ideas?
Reply
#2
This works for me
myfunc.py in myfuncs folder
def print_it(arg):
    return arg
test.py
from myfuncs import myfunc
print(myfunc.print_it('Hello World!'))
Output:
Hello World!
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
Scordomaniac Wrote:but it gets tricky when i attempt to do it from a different directory...
You most think of where Python look for files/folders when not is same folder as code you run
Python look for files/folders in sys.path,so get in trouble if top level folder is not in sys.path.
look at how Packages works or some of my example here
In all my example so is top level folder in sys.path or run from virtual environment.
Reply
#4
I am always making little functions.

When I have a lot of functions for 1 specific purpose, I save them as a module, then they are all available for that purpose.

For example, guiHTML.py has lots of functions for making html.

import os, sys    
# to import the files we need the paths
path = '/home/pedro/myPython/myModules/'

# append the paths
sys.path.append(path)

import tkinter as tk
    
from functools import partial      
import guiHTML
After that, all the functions are available.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can i combine these two functions so i only open the file once? cubangt 4 850 Aug-14-2023, 05:04 PM
Last Post: snippsat
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 3,291 Jun-27-2023, 01:17 PM
Last Post: diver999
  Split Bytearray into separate Files by Hex delimter lastyle 5 2,624 Mar-09-2023, 07:49 AM
Last Post: bowlofred
  Can I get some clarification on importing functions from external files. wh33t 3 896 Feb-25-2023, 08:07 PM
Last Post: deanhystad
  Extract file only (without a directory it is in) from ZIPIP tester_V 1 976 Jan-23-2023, 04:56 AM
Last Post: deanhystad
  How to separate the values in an element to add them monty024 4 1,011 Jan-23-2023, 04:28 AM
Last Post: deanhystad
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,110 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  no such file or directory in SFTP saisankalpj 2 1,534 Nov-25-2022, 11:07 AM
Last Post: DeaD_EyE
  New2Python: Help with Importing/Mapping Image Src to Image Code in File CluelessITguy 0 718 Nov-17-2022, 04:46 PM
Last Post: CluelessITguy
  python standard way of importing library mg24 1 891 Nov-15-2022, 01:41 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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