TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to PYTHON

Python Import Syntax Examples: Modules, NameError

Understand the import statement and the from keyword. Use star syntax.
Import. Some programs use no import statements. But often we require access to external packages and their modules. With the from and import directives, we include those names into a program. This avoids errors.Error
Example. This program requires the date and timedelta modules from the datetime package. It signals this by using an asterisk (star) after the import keyword. This means "import all." All submodules of datetime are imported.

Caution: This program is correct. But if you remove the "from import" statement, it will result in NameErrors.

Python program that uses import star syntax from datetime import * def yesterday(): today = date.today() yesterday = today - timedelta(days=1) return yesterday print(date.today()) print(yesterday()) Output 2014-04-17 2014-04-16
The star syntax is not the only option. We can specify the modules directly, by naming them. This may be preferred in many projects. It makes clear which modules are being used. This may be less complex.

Tip: We can specify multiple modules from one package using a comma between each name. This reduces the line count of the program.

Alternate syntax 1: Python from datetime import date, timedelta Alternate syntax 2: Python from datetime import date from datetime import timedelta
NameError. A NameError is often caused by a missing import statement. Consider this program. It correctly imports date, but lacks the timedelta import that it needs. This results in a NameError being thrown. Try python, Exceptions.

However: The program fails at the line where we assign "yesterday." The print statement is never reached.

Print
Python program that causes NameError from datetime import date today = date.today() yesterday = today - timedelta(days=1) print(yesterday) Output Traceback (most recent call last): File "C:\programs\file.py", line 8, in <module> yesterday = today - timedelta(days=1) NameError: name 'timedelta' is not defined
Custom. Here we use a custom Python module. Please create a new Python file in the same directory as your Python program. Give it a name. Then in the Python program, use the import statement to include it. You can call methods in the module.

Tip: The module file must be in the same directory as your program. And its name must equal the name in your "import" statement.

Example file, stock.py: Python def buy(): print("stock.buy called") Python program that uses stock module, file.py import stock # Call the method in the stock module. stock.buy() Output stock.buy called
Summary. Python programs can become complex. They are not always simple scripts. With the import statement, and its associated keyword "from," we bring in external package resources to our program.Built-ins
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf