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.
Related
first post here so sorry if it's hard to understand. Is it possible to shorten the directory in python to the location of the .py file?. For example, if I wanted to grab something from the directory "C:\Users\Person\Desktop\Code\Data\test.txt", and if the .py was located in the Code folder, could I shorten it to "\data\test.txt". I'm new to python so sorry if this is something really basic and I just didn't understand it correctly.
I forgot to add i plan to use this with multiple files, for example: "\data\test.txt" and \data\test2.txt
import os
CUR_FILE = os.path.abspath(__file__)
TARGET_FILE = "./data/test.txt"
print(os.path.join(CUR_FILE, TARGET_FILE))
With this, you can move around your Code directory anywhere and not have to worry about getting the full path to the file.
Also, you can run the script from anywhere and it will work (you don't have to move to Code's location to run the script.
You can import os and get current working directory ,this will give you the location of python file and then you can add the location of folder data and the file stored in that ,code is given below
import os
path=os.getcwd()
full_path1=path+"\data\test.txt"
full_path2=path+"\data\test2.txt"
print(full_path1)
print(full_path2)
I think this will work for your case and if it doesn't work then add a comment
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?
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)
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...