Python Forum
How to import file and function in another folder
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to import file and function in another folder
#1
Hi,
I have two folders folder1, folder2 as shown below:

Folder1
-- file1
--file2
Folder2
--file3
-- file4
I open file4 in (spyder/vscode) and want to import file2 & function2. But I am unable to open, it give Folder1 does not exists. Because when I open file4, the cwd is Folder2. How to open file in another folder.
Reply
#2
The simplest way is to append folder1 to the list sys.path
import sys
sys.path.append('path/to/folder1')

import file2
Then you could read the section about modules in the python tutorial https://docs.python.org/3/tutorial/modules.html
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 525 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  problem in import module from other folder akbarza 5 1,389 Sep-01-2023, 07:48 AM
Last Post: Gribouillis
  Reading a file name fron a folder on my desktop Fiona 4 894 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
Video doing data treatment on a file import-parsing a variable EmBeck87 15 2,812 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Import XML file directly into Excel spreadsheet demdej 0 836 Jan-24-2023, 02:48 PM
Last Post: demdej
  Function not executing each file in folder mathew_31 9 2,234 Aug-22-2022, 08:40 PM
Last Post: deanhystad
  Dynamic File Name to a shared folder with open command in python sjcsvatt 9 6,020 Jan-07-2022, 04:55 PM
Last Post: bowlofred
  Code to check folder and sub folders for new file and alert fioranosnake 2 1,930 Jan-06-2022, 05:03 PM
Last Post: deanhystad
  How from sklearn.datasets import load_diabetes change to import to CSV file Anldra12 0 1,850 Dec-25-2021, 07:20 PM
Last Post: Anldra12
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,471 Dec-18-2021, 09:32 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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