Feb-18-2018, 07:57 PM
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?
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?