Import a File from a Subdirectory

Import a file from a subdirectory?

Take a look at the Packages documentation (Section 6.4).

In short, you need to put a blank file named

__init__.py

in the lib directory.

Importing from subfolder in Python

Note that Folder1 is a directory, the .py scripts are your modules.

In pyscript3 you should be able to go:

from Folder1 import pyscript1

Then you can access a method with name methodname like:

pyscript1.methodname()

Otherwise you can import the method directly like:

from Folder1.pyscript1 import methodname

and use it like:

methodname()

EDIT:

For your program to see Folder1 and Folder2, you need to run your program from the MainProject folder.

Either move pyscript3 to your MainFolder or write another script, let's call it main.py, and call the necessary code to instantiate the class/call the function you want in pyscript3.py.

To summarize, you always want to run the entry module from the base folder of your project.

How to Import module or a file from subfolder in Python?

After Doing some R&D, Have done it
Just need to create __init__.py (a empty py file) and add it to all subdirectory
such as

 /pyproject/
api/
__init__.py
gmail/
__init__.py
abc.py

facebook/
__init__.py
xyz.py

And add in main py file

from api.gmail.abc import *
or
import api.gmail.abc

How to import a module from a subfolder in python3 (with empty __init__.py)

Let's suppose we have this folders/files architecture:

test
├── callfoo.py
└── folder
├── __init__.py
└── submodule.py

1 directory, 3 files

callfoo.py:

from folder.submodule import foo

def main():
foo()

if __name__ == '__main__':
main()

submodule.py:

def foo():
print('foo in submodule')

now place your self at the same level's folder of callfoo.py and run:

$ python3 callfoo.py

Output:

> foo in submodule

Importing a file in the same sub-directory of a module

Python follows an execution model very close to regular programs on your computer. The program (or python script) is located somewhere on the $PATH and any libraries it loads (or python module) is in some different system defined location. Python intends to be installed. Scripts are placed on the PATH and modules are buried somewhere in the python directories.

One exception is that when you run a script, python adds its directory to the python path for modules. So any of its subdirectories that happen to have an __init__.py become python packages. When you run main.py, it is a script and subdir is a package. That lets module1.py do package relative imports. Interestingly, main.py itself isn't in a package, the __init__.py at its level is not imported.

When you run module1.py as a script, it isn't a package either. So package relative imports no longer work.

The solution is to go through the pain of making all of this an installable package. You define a setup.py and, well, there are multiple options on how to set things up there. setuptools is a good resource. One thing to notice is the console_scripts parameter that will auto-generate scripts from module entry points.

Its a large hill to climb but as projects go beyond one or two modules, worth the pain.

Import module from subfolder

There's no need to mess with your PYTHONPATH or sys.path here.

To properly use absolute imports in a package you should include the "root" packagename as well, e.g.:

from dirFoo.dirFoo1.foo1 import Foo1
from dirFoo.dirFoo2.foo2 import Foo2

Or you can use relative imports:

from .dirfoo1.foo1 import Foo1
from .dirfoo2.foo2 import Foo2

python: importing files to another sub directory

You should join absolute path of my_dir, which is data_reader's parent to the system path at the beginning of file analyzer.py
Here is the example.

# analyzer.py
import sys
from pathlib import Path
parent_of_data_reader = str(Path('../../').absolute())
sys.path.insert(0, parent_of_data_reader)
# import hello from module data_reader
from data_reader import hello

Note: hello is a variable declared in my_dir/data_reader/__init__.py. If you want you want to import things from my_dir/data_reader/reader.py, try

# analyzer.py
import sys
from pathlib import Path
parent_of_data_reader = str(Path('../../').absolute())
sys.path.insert(0, parent_of_data_reader)
# import hello from module data_reader.reader
# hello is a variable declared in reader.py
from data_reader.reader import hello

Then you will be able to import your module without error.

How to import from a file in a subfolder of src?

The subfolder must be declared as a module. You can do that using 3 different ways:

  • Inline: declare the sorting_algorithms module inside your main.rs:

    // In main.rs:

    mod sorting_algorithms {
    pub mod bubble;
    }

    This is the simplest in my opinion.

  • Put a sorting_algorithms.rs into the src folder, with the module declaration:

    // In sorting_algorithms.rs:

    pub mod bubble;
  • Put a mod.rs file with the above content into the subfolder. This is advised against, because it can be confusing to have several mod.rs file to work with.



Related Topics



Leave a reply



Submit