Importing files from different folder

Posted on

Solving problem is about exposing yourself to as many situations as possible like Importing files from different folder and practice these strategies over and over. With time, it becomes second nature and a natural way you approach any problems in general. Big or small, always start with a plan, use other strategies mentioned here till you are confident and ready to code the solution.
In this post, my aim is to share an overview the topic about Importing files from different folder, which can be followed any time. Take easy to follow this discuss.

Importing files from different folder

I have the following folder structure.

├── app
│   └── folder
│       └──
└── app2
    └── some_folder

I want to import some functions from in

I’ve tried

from import func_name

and some other various attempts but so far I couldn’t manage to import properly. How can I do this?

Asked By: Ivan


Answer #1:

Note: This answer was intended for a very specific question. For most programmers coming here from a search engine, this is not the answer you are looking for. Typically you would structure your files into packages (see other answers) instead of modifying the search path.

By default, you can’t. When importing a file, Python only searches the directory that the entry-point script is running from and sys.path which includes locations such as the package installation directory (it’s actually a little more complex than this, but this covers most cases).

However, you can add to the Python path at runtime:

import sys
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, '/path/to/application/app/folder')
import file
Answered By: Cameron

Answer #2:

Nothing wrong with:

from import func_name

Just make sure folder also contains an, this allows it to be included as a package. Not sure why the other answers talk about PYTHONPATH.

Answered By: joey

Answer #3:

When modules are in parallel locations, as in the question:


This shorthand makes one module visible to the other:

import sys
Answered By: slizb

Answer #4:

First import sys in

 import sys

Second append the folder path in

sys.path.insert(0, '/the/folder/path/name-package/')

Third Make a blank file called __ init in your subdirectory (this tells Python it is a package)

  • name-package
    • __ init

Fourth import the module inside the folder in

from name-package import name-module
Answered By: Alex Montoya

Answer #5:

I think an ad-hoc way would be to use the environment variable PYTHONPATH as described in the documentation: Python2, Python3

# Linux & OSX
# Windows
set PYTHONPATH=C:pathtodirWithScripts;%PYTHONPATH%
Answered By: Ax3l

Answer #6:

Your problem is that Python is looking in the Python directory for this file and not finding it. You must specify that you are talking about the directory that you are in and not the Python one.

To do this you change this:

from import func_name

to this:

from import func_name

By adding the dot you are saying look in this folder for the application folder instead of looking in the Python directory.

Answered By: CianB

Answer #7:

The answers here are lacking in clarity, this is tested on Python 3.6

With this folder structure:
---- myfolder/

Where has the content:

def myfunc():

The import statement in is:

from myfolder.myfile import myfunc

and this will print hello.

Answered By: danday74

Answer #8:

From what I know, add an file directly in the folder of the functions you want to import will do the job.

Answered By: Vaibhav Singh

Leave a Reply

Your email address will not be published. Required fields are marked *