Python Forum
Importing module from outside of the current directory
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Importing module from outside of the current directory
#1
Hi guys,

If I'm in say /home/a
and I want to import a module name "file.h"  in   /home/b/c/d
I tried :


import sys
sys.path.insert(0, '/home/b/c/d')
but when I did

import file.h
It still says no module named file.h,even though file.h is indeed in the /home/b/c/d directory ?

What did I miss?

Oh, I should add that this file is not a python file, is it possible to do this ?
Reply
#2
sys.path search for files with suffix .py only.
Quote:Oh, I should add that this file is not a python file, is it possible to do this ?
You most explain what you trying do eg "i want call/run C code from Python".
Suffix .h is a header file for C.
Reply
#3
Thank you, yeah, of course it's not possible...directly. There is something I didn't set up correctly.
Reply
#4
What are you storing in file.h? If its just constant values you should use something like JSON or similar. 

If you really need to create a C extension you should check out a tutorial to do so
https://python-forum.io/Thread-Creating-...ons-Part-1
Recommended Tutorials:
Reply
#5
Thank you, I believe that is a program.
I think there is a way to set up that in the package I'm using.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  importing variables from module 8376459 1 269 Feb-18-2024, 02:24 PM
Last Post: deanhystad
  no module named 'docx' when importing docx MaartenRo 1 846 Dec-31-2023, 11:21 AM
Last Post: deanhystad
  My code displays too much output when importing class from a module lil_e 4 1,153 Oct-22-2022, 12:56 AM
Last Post: Larz60+
  Get Current Directory From ShortCut. Oshadha 1 955 Jul-30-2022, 04:41 PM
Last Post: snippsat
  importing functions from a separate python file in a separate directory Scordomaniac 3 1,365 May-17-2022, 07:49 AM
Last Post: Pedroski55
  No Module found in other directory than source code [SOLVED] AlphaInc 1 2,041 Nov-10-2021, 04:34 PM
Last Post: AlphaInc
  Importing module in jupyter Noteboook ajitnayak1987 0 1,741 Jun-04-2021, 12:26 PM
Last Post: ajitnayak1987
  ERROR: importing desired module mbgamer28 0 1,674 Apr-05-2021, 07:46 PM
Last Post: mbgamer28
  importing module - not working jdhamblett 3 3,002 Jun-22-2020, 07:33 PM
Last Post: jdhamblett
  importing same python library in multiple custom module escape_freedom13 6 3,792 May-10-2020, 07:01 PM
Last Post: escape_freedom13

Forum Jump:

User Panel Messages

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