- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to import other Python files?
To use any package in your code, you must first make it accessible. You have to import it. You can't use anything in Python before it is defined. Some things are built in, for example the basic types (like int, float, etc) can be used whenever you want. But most things you will want to do will need a little more than that. For example, if you want to calculate cosine of 1 radian, if you run math.cos(0), you'll get a NameError as math is not defined.
You need to tell python to first import that module in your code so that you can use it.
>>> math.cos(0) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'math' is not defined >>> import math >>> math.cos(0) 1.0
If you have your own python files you want to import, you can use the import statement as follows:
>>> import my_file # assuming you have the file, my_file.py in the current directory. # For files in other directories, provide path to that file, absolute or relative.
- Import Files and Images in ReactJS
- How to import Matplotlib in Python?
- How can we import CSV files into MySQL tables by using mysqlimport?
- How to convert PDF files to Excel files using Python?
- Import module in Python
- How to compare files in Python
- How I can dynamically import Python module?
- How to use multiple modules with Python import Statement?
- How to install and import Python modules at runtime?
- How to import everything from a python namespace / package?
- How to import Pandas package?
- How to monitor Python files for changes?
- How to remove swap files using Python?
- How to create powerpoint files using Python
- How to Crack PDF Files in Python?