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()
Related
I work with Django Framework, and I try to open a Python file from outside the Django package.
I use the OS library to get to the path that is outside the Django package, like this:
file_path = OS.path.join(settings.FILES_DIR, 'execute_get_symbols.py')
In file_path I got only the file path
I want to run a function that is inside the file "execute_get_symbols.py".
My questions are:
It is possible?
And if it is possible, so how.
And if it's not possible, So... how can I import files that are outside the Django package and execute the function?
Like from package.file_name import function
You can imoprt your file by adding it to sys.path (the list of paths python looks at to import things) - i believe that this is a kinda hacky way but still commonly used by django users:
import sys
sys.path.append(path_to_your_file)
import your_file
Afterwards you should be able to use it normally
I added the following code in settings file on Django:
sys.path.append(os.path.join(BASE_DIR.parent.parent, "")) # To get the path from root until current directory
And because of that, all the packages recognize on run time.
I have these files:
/somewhere/file.py
/elsewhere/file.py
/completeley/different/place/main.py
Now, within main.py I need stuff from the other files, in pseudocode:
from "/somewhere/file.py" import function_1
from "/elsewhere/file.py" import function_2
Can Python handle this? How?
Note that the trick of placing the empty __init__.py file cannot be used here (or at least I don't know how, also don't want to convert the whole file system into a Python package), and using sys.path.insert also fails because the two files have the same name.
This should work.
from somewhere.file import function_1
from somewhere_else.file import function_2
If you want to import the modules
import somewhere.file as file_a
import somewhere_else.file as file_b
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?
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]
I am trying to execute one python script (file2.py) through the other (file1.py).
The first file is located in test/file1.py and the second file in test/test1/file2.py.
As an example file2.py has the following code:
file = open("textfile.txt","w")
file.write("Hello world")
file.close()
so I am trying to create a .txt file from file1.py.
To do so, I have tried several solutions such as:
import Test1.file2
or
import os
os.system('Test1/file2.py')
or
from subprocess import call
call('Test1/file2.py', shell='True')
in file1.py but none of them seem to work. The first solution works well if I want to import a function but it does not work when I want to create a text file.
Does anyone know what I am doing wrong and how to fix it?
To run the other file, you have some options:
The best one is to treat the file as a module, import it, and call it's methods, so you would have to encapsulate the .txt creating inside a method
#on file1.py
def createTxt():
#your txt creation goes here
and call createTxt from file2.py importing file1.py, which will run the method and create the txt file
#on file2.py
import file1
createTxt()
You can also use execfile('file2.py') to execute it and run it as a script or make a system call with os.system('python file2.py') importing os to also run as a script
The first one is safer and a better code in general, avoid using the other two when possible