Access function in other python file within a subfolder - python

I am building a fairly complex python app with various scripts. I am planning on sorting these scripts into subfolders so I can then access the functions from a main file. For example having a function called main inside a file called test.py inside a subfolder called test. How would I call the function?
I have tried using
from test import main
However I get a ModuleNotFoundError error.
Thanks in advance for the help.

As mentioned here, to import files from subfolders in python, use the '.' to signify a subdirectory.
In your example, the import would be
from test.test import main
The general syntax for such an import would be the following:
from [subfolderName].[fileName] import [functionName]
To import all functions from file, the syntax would be:
import [subfolderName].[fileName]

Related

How to import class from other directory following this structure python

Reading other posts I have structured my project as below:
/__init__.py
/src/main.py
/src/search-tree/node.py
/src/search-tree/multi_child_node.py
/src/utils/node_generator.py
Now when I'm inside node_generator.py I would like to import multi_child_node.py and use its class but I don't know how to do it, I have tried from .x import y but nothing. I'm new in python so maybe I'm missing something very simple.
Actually, the file structure depends on where do you run your main file, not the file itself.
For example, if your main.py calls node_generator.py like this
from utils import node_generator
and then node_generator.py calls multi_child_node.py, it should call it as it was called from main.py:
from search_tree import multi_child_node
P.S, you shouldn't use dash - or spaces in the naming for your module, but if you have to, check out the answer for this question How to import module when module name has a '-' dash or hyphen in it?

How do you use multiple .py files in Python?

I'm messing around with some Python files and I want to know if you can and how you can reference an external file like in other coding languages so that I can make an application with multiple .py files. I tried looking but I couldn't really understand what it was saying and it wasn't really what I was looking for.
You can import files at the top of your python file.
And then you can run them by calling the function.
import sample # import sample.py file
import other # import other.py file
import another # import another.py file
sample.example()

How can I put functions in different files, but get them all with just one import?

Instead of creating a module.py (containing all the functions), can I create a folder MODULE and collect all the functions in different files?
I'd like to do that in a way that main.py contains import MODULE and, if it's possible, to call the functions directly (fun_1(), fun_2()) without the nomenclature MODULE.fun_1(), MODULE.fun_2(), etc.
I think the only right way is creating __init__.py (import all functions contained in other files) in your MODULE folder. And use statement from MODULE import *. If you wanna use import MODULE and call func in other files then, that never work. Interpreter will raise the NameError, cause there are no the variables.
__init__.py file like this:
from file1 import func_1
from file2 import func_2
Yes.
Create your main file, main.py along with another file where you want to put another function. I will use the other.py file.
In main.py, write
from other import *
Note the missing .py, Python handles that for you.
In other.py, write
def your_function():
return -1
You can have as many functions in here as you want. Now, you can call your_function() in main.py.
Example Repl.it: https://repl.it/repls/MeaslyBurdensomeDesigners

Call a Python script from a different Python script located at a different location

I have a Python script which is calling a class of another Python script.
The structure of these two scripts are as follows:
c:\testing\sample.py
c:\testing\example\demo\projects\module.py
Now inside sample.py I am calling module.py as follows:
from "c:/testing/example/demo/projects/module.py" import module_C
When I execute this portion of sample.py I get an error There is an error in program and this error occurs in its first line only.
How shall I call the module_C inside sample.py?
the from/import syntax does not work with files, only with modules.
To achieve what you want, there are 2 possibilities
first one: turn traversing directories into modules:
create empty __init__.py files in c:/testing/example/demo, and c:/testing/example/demo/projects,
then from demo.projects.module import module_C
second one: add path dynamically:
import sys,os
sys.path.add(os.path.join(os.path.dirname(__file__),"demo/projects"))
from module import module_C
knowing where you are running, use current module directory and add the demo/projects absolute path: it works because the paths have a common prefix)

Python import module from relative folder

i'm creating a python program, and i want to split it up into seperate files. I'm using import to do this, but its not working (specifically, a variable is stored in one python file, and its not being read by the main one.
program /
main.py
lib /
__init__.py
config.py
functions.py
I have in main.py:
import lib.config
import lib.functions
print(main)
and config.py has
main = "hello"
I should be getting the output "hello", when i'm executing the file main.py, but i'm not. I have the same problem with functions stored in functions.py
Any help would be great,
Importing the module with a simple import statement does not copy the names from that module into your own global namespace.
Either refer to the main name through attribute access:
print(lib.config.main)
or use the from ... import ... syntax:
from lib.config import main
instead.
You can learn more about how importing works in the Modules section of the Python tutorial.

Categories