Python Forum
Seeking understanding with the python import function.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Seeking understanding with the python import function.
#1
I am a beginner Python programmer, and I am now learning about the import sys. My primary influence is a program that I have been working on for a bit now, which I feel like needs to be broken down into several different modules.

I started reading this article online which was an intro to using the import sys, and I ran some code examples to confirm my understanding of the topic. I created the following module, from a file I called Testy.py:

>>> a=11
>>> b=3
>>> def add(a,b):
"""This program adds two numbers and return the result."""
result=a+b
return result

The function ran fine from it's own module. Later I tried importing and running all the statements from Testy in a different module, which i will refer to as "Scripty".

It didn't work how I assumed it would work. I initially added a line to Scripty which imported Testy. I then tried to call the function add(a,b) from within Scripty, I kept getting errors saying "a is not defined", and "b is not defined".

After trial and error, moving this line around and that line, I connected the dots and realized that by me simply stating "import Testy" I wasn't getting everything I needed.

I then added lines to scripty stating:

>>>from Testy import a
>>>from Testy import b

Everything worked perfectly after that, I got the rest of the code I needed.

My question for the community is, what exactly does a simple import statement do? Why did I have to specify on different lines to import certain objects/statements. Do I have to do this, is there a simpler way to get all the statements from a module?
Reply
#2
You could do a dissertation on import, but shy of that, what better way to expose yourself than
a video by the guy that rewrote import for python 3, Brett Cannon: https://www.youtube.com/results?search_q...ett+Cannon
It is quite complicated (but also quite interesting), so put your seat-belt on.
Reply
#3
To fix it so it make sense.
There should be no >>> in code that's imported.
# foo.py
def add(a, b):
    """This program adds two numbers and return the result."""
    result = a + b
    return result
So now i want to use add function in an other script.
# bar.py
from foo import add

a = 11
b = 3
print(add(a, b))
Output:
14
You can look at my tutorial here,it's a little heavy as it cover a lot Module, Package, Wheel, setup.py, and sharing on PyPi.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Seeking advice on dask distributed sawtooth500 4 231 Apr-15-2024, 11:07 PM
Last Post: sawtooth500
  Understanding a function ebolisa 3 782 Jul-14-2023, 06:03 PM
Last Post: snippsat
  Understanding venv; How do I ensure my python script uses the environment every time? Calab 1 2,255 May-10-2023, 02:13 PM
Last Post: Calab
  New to python/coding Need help on Understanding why this code isn't working. Thanks! mat3372 8 1,740 May-09-2023, 08:47 AM
Last Post: buran
  Understanding Python classes PythonNewbee 3 1,182 Nov-10-2022, 11:07 PM
Last Post: deanhystad
  How to import file and function in another folder SriRajesh 1 3,150 Dec-18-2021, 08:35 AM
Last Post: Gribouillis
  Understanding Python super() for classes OmegaRed94 1 1,826 Jun-09-2021, 09:02 AM
Last Post: buran
  Better Understanding Of Object Orientation In Python JoeDainton123 3 2,462 Aug-30-2020, 02:49 PM
Last Post: deanhystad
  Having hard time understanding the function self-returning itself twice jagasrik 2 2,479 Aug-15-2020, 08:50 PM
Last Post: deanhystad
  New user seeking help EdRaponi 2 43,290 Jun-23-2020, 12:03 PM
Last Post: EdRaponi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020