Change language

How do I import other Python files?

|

To use any package in your code, you must first make it available. You have to import this. You cannot use anything in Python until it is defined. Some things are built in, for example basic types (like int, float, etc.) can be used at any time. 

But most of the things you want to do will take a little more than that. For example, if you want to calculate the cosine of 1 radian, if you run math.cos (0), you will get a NameError since the math is undefined.



Example

You must tell python to first import this module into your code so 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 that you want to import, you can use the import statement like this:

""" 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. 
< / p>



How do I import other Python files? _files: Questions

How do I import other Python files? Python functions: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method