Is it possible to run another Python File from a Main Python File.
For example:
I have a file Main.py and its servers as the main directory for the following files:
Surface Area Calculator
Scientific Calculator
How would you import a Python file to them. I was hoping to do this so I can keep myself organized.
Could that possible open a file with a .txt format but written in python?
Thanks
You don't need to point a Python file to them, you just need the import keyword to do that.
So, from your Main.py file you can do:
from other_python_file import SurfaceAreaCalculator, ScientificCalculator
This way you will "run" other_python_file.py file within Main.py.
If you want to run content from a TXT file, I guess you could use eval or execfile to do that. Don't know if it is such a good idea though...
Related
I've got a task to do that is crushing my head. I have five .py documents and I want to make a menu in another .py so I can run any of them by introducing a string inside an input() but don't really see the way to do that and I don't know if there is somehow I can.
I have tried import every file to the 6th file but I don't even know how to start.
I would like it just to be seen as simple as it can sound, but yet I find it really hard.
If you just want to run them, then try this:-
import os
file_path = input("Enter the path of your file = ")
os.system(file_path)
If the file that you are trying to execute is not in the current
directory, i.e. doesn't exist in the same folder as the currently
executing python file, then you have to provide it's full path.
Path Format:-. C:\Users\lmYoona\OneDrive\Desktop\example.py
If the python file you are trying to execute is in the same directory as
the currently executing python file, then abstract name will also
work
Path Format:- example.py
P.S.:- I would only recommend this method if all you want is just to execute the other python file, rather then importing stuff from it.
Let's say I have a folder containing the main-file main.py and a configuration-file config.json.
In the JSON-file there are multiple entries containing different filenames and the name of a method within each file.
Depending on user input from main.py I get the corresponding filename and the method from config.json and now I want to execute that method.
I tried the following
file = reports [id]["filename"]
method = reports [id]["method"]
# which would give me: file = 'test.py' and method = 'execute'
import file
file.method()
This didn't work obviously. The problem is that I can't know which files there are during compilation.
Other developers would add scripts to a specific folder and add their entries to the configuration file (kind of like a plugin). Does anybody have a solution here?
Thanks in advance
Thanks for your input... importlib was the way to go. I used this answer here:
How to import a module given the full path?
So I have an assignment, and For a specific section, we are supposed to import a .py file into our program."You will need to import histogram.py into your program."
Does that simply mean to create a new python file and just copy and past whatever is in the histogram.py into the file?
This part of my assignment is to create a graphical display with the contents in the .py file (which confuses me too) I was reading the chapters from the tb and it states how to create a window, but I havent seen anything about importing.. Sorry if this is a dumb question
You have to create your own python file and simply add
import histogram
at the top of the file (or lets say in the "import" section).
Yout don't need the .py extension. Make sure the histogram.py file is in the same folder like your created py file.
You need to make sure you have the files you need before you try and import them, either downloading them or getting them through pyget. Once you have your files then you need to call them (2.7 docs)
from .foo import Foo
from .bar import Bar
import Fizz
All you have to do is paste the file named histogram.py to your working directory and to use the file on your code just type import histogram on the top most line of your main code file then you can use anything(functions and variables) from histogram.py.
I am working in a set of directories with the following structure:
Master/
Subfolder_1/file_1_1.py
file_1_2.txt
Subfolder_2/file_2_1.py
I import file_1_1 in file_2_1 as follows:
import sys
sys.path.append('../file_1_1')
file_1_1 is reading file_1_2.txt which is in the same directory. However, when I call the function that reads file_1_2.txt from file_2_1.py, it says no such file and directory and it gives me the path of file_1_2.txt as:
Master/Subfolder_2/file_1_2.txt
which is a wrong path. It seems like python in this case is using the working directory as a reference. How can I solve this error given that I don't want to include the absolute path for each file I read.
Don't mess with sys.path, and don't think of imports as working against files and directories. Ultimately everything has to live in a file somewhere, but the module hierarchy is a little more subtle than "replace dot with slash and stick a .py at the end".
You almost certainly want to be in Master and run python -m Subfolder_1.file_1_1. You can use pkg_resources to get the text file:
pkg_resources.resource_string('Subfolder_1', 'file_1_1.txt')
info=[]
with open(os.path.realpath('yourtextfile.txt','r') as myfile:
for line in myfile:
info.append(line)
I have a rather simple program that writes HTML code ready for use.
It works fine, except that if one were to run the program from the Python command line, as is the default, the HTML file that is created is created where python.exe is, not where the program I wrote is. And that's a problem.
Do you know a way of getting the .write() function to write a file to a specific location on the disc (e.g. C:\Users\User\Desktop)?
Extra cool-points if you know how to open a file browser window.
The first problem is probably that you are not including the full path when you open the file for writing. For details on opening a web browser, read this fine manual.
import os
target_dir = r"C:\full\path\to\where\you\want\it"
fullname = os.path.join(target_dir,filename)
with open(fullname,"w") as f:
f.write("<html>....</html>")
import webbrowser
url = "file://"+fullname.replace("\\","/")
webbrowser.open(url,True,True)
BTW: the code is the same in python 2.6.
I'll admit I don't know Python 3, so I may be wrong, but in Python 2, you can just check the __file__ variable in your module to get the name of the file it was loaded from. Just create your file in that same directory (preferably using os.path.dirname and os.path.join to remain platform-independent).