Create module - Lab
Step 1: Run following command in your terminal
$ python3 -c "import site; print(site.getsitepackages())"
You should get an output that looks like this:
['/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages', '/Library/Frameworks/Python.framework/Versions/3.4/lib/site-python', '/Library/Python/3.4/site-packages']
Step2: Download the file below
$ python3 -c "import site; print(site.getsitepackages())"
You should get an output that looks like this:
['/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages', '/Library/Frameworks/Python.framework/Versions/3.4/lib/site-python', '/Library/Python/3.4/site-packages']
Step2: Download the file below
mysearch.py |
Open the file and understand the content - there is:
data_list variable, containing quotes,
search function, that performs search over data_list
and we also execute the search function for a hardcoded* string (if you don't know what hardcoded means, please ask to explain)
Run the file and see how it works
python3 mysearch.py
Step3: Go to the Python library folder, use the path you got above:
[' /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/ site-packages', '/Library/Frameworks/Python.framework/Versions/3.4/lib/site-python', '/Library/Python/3.4/site-packages']
Copy your mysearch.py there or place it in the same directory where your program is
We just created a Python module, now we will try to use it.
Step4: Open python in terminal, follow the blue italic commands
We will now try to use our module,
Let's try to call data_list variable:
>>> data_list
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'data_list' is not defined
It doesn't work, because we did not explain where we are getting the data_list from to Python,
so let's do that:
>>> import mysearch
Found at 10 If you love a flower that lives on a star, it is sweet to look at the sky at nig
Found at 31 For millions of years flowers have been producing thorns. For millions of years
Found at 44 Of course, I love you,' the flower said to him. 'If you were not aware of it, it
Found at 77 If someone loves a flower of which just one exists among all the millions and mi
Found at 81 If you love a flower that lives on a star, then it's good at night, to look up a
None
You should see the output you saw earlier when you ran the mysearch.py, this is because Python now loaded/executed your module and should now be aware of it's content. Let's test that:
>>> data_list
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'data_list' is not defined
Still doesn't work - this is because Python is not sure which data_list we are talking about - we have to specify the module name we are importing it form:
>>> mysearch.data_list
['And now here is my secret, a very simple secret: It is only with the heart that one can see rightly; what is essential is invisible to the eye.', 'All grown-ups were once children... but only few of them remember it.', 'People ...
>>> mysearch.search("someone")
Found at 20 When someone blushes, doesn't that mean 'yes'?
Found at 31 For millions of years flowers have been producing thorns. For millions of years
Found at 77 If someone loves a flower of which just one exists among all the millions and mi
>>>
Exit out of Python to clear it's working memory:>>> exit()
Go back to continue:
>>> python3
We can also explicitly import just a specific component, make sure Python has no memory of data_list
>>> data_list
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'data_list' is not defined
>>> mysearch.data_list
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'mysearch' is not defined
>>>
Now, run:
>>> from mysearch import data_list
Found at 10 If you love a flower that lives on a star, it is sweet to look at the sky at nig
Found at 31 For millions of years flowers have been producing thorns. For millions of years
Found at 44 Of course, I love you,' the flower said to him. 'If you were not aware of it, it
Found at 77 If someone loves a flower of which just one exists among all the millions and mi
Found at 81 If you love a flower that lives on a star, then it's good at night, to look up a
None
>>> data_list
['And now here is my secret, a very simple secret: It is only with the heart that one can see rightly; what is essential is invisible to the eye.', 'All grown-ups were once children... but only few of them remember it.', 'People have ...
Because we did not import search function, Python has no knowledge of it
>>> search("something")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'search' is not defined
>>> mysearch.search("something")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'mysearch' is not defined
Let's import both search and data_list:
>>> from mysearch import data_list, search
>>> search("draw")
Found at 79 Will you draw me a sheep?
>>> exit()
Return back to Python:
python3
>>> from mysearch import *
Call your search function and print out data_list
Step5. Create a new module - create a new module, name it to your liking, place a simple variable and a simple function in it, follow same steps above to import it and to use your variable and function
Step6. Run following
>>> import sys
>>> sys.path
You should see something similar to output below, those are the paths where Python will be looking for the libraries
['', '/usr/local/lib/python2.7/site-packages', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python34.zip', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plat-darwin', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages']
data_list variable, containing quotes,
search function, that performs search over data_list
and we also execute the search function for a hardcoded* string (if you don't know what hardcoded means, please ask to explain)
Run the file and see how it works
python3 mysearch.py
Step3: Go to the Python library folder, use the path you got above:
[' /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/ site-packages', '/Library/Frameworks/Python.framework/Versions/3.4/lib/site-python', '/Library/Python/3.4/site-packages']
Copy your mysearch.py there or place it in the same directory where your program is
We just created a Python module, now we will try to use it.
Step4: Open python in terminal, follow the blue italic commands
We will now try to use our module,
Let's try to call data_list variable:
>>> data_list
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'data_list' is not defined
It doesn't work, because we did not explain where we are getting the data_list from to Python,
so let's do that:
>>> import mysearch
Found at 10 If you love a flower that lives on a star, it is sweet to look at the sky at nig
Found at 31 For millions of years flowers have been producing thorns. For millions of years
Found at 44 Of course, I love you,' the flower said to him. 'If you were not aware of it, it
Found at 77 If someone loves a flower of which just one exists among all the millions and mi
Found at 81 If you love a flower that lives on a star, then it's good at night, to look up a
None
You should see the output you saw earlier when you ran the mysearch.py, this is because Python now loaded/executed your module and should now be aware of it's content. Let's test that:
>>> data_list
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'data_list' is not defined
Still doesn't work - this is because Python is not sure which data_list we are talking about - we have to specify the module name we are importing it form:
>>> mysearch.data_list
['And now here is my secret, a very simple secret: It is only with the heart that one can see rightly; what is essential is invisible to the eye.', 'All grown-ups were once children... but only few of them remember it.', 'People ...
>>> mysearch.search("someone")
Found at 20 When someone blushes, doesn't that mean 'yes'?
Found at 31 For millions of years flowers have been producing thorns. For millions of years
Found at 77 If someone loves a flower of which just one exists among all the millions and mi
>>>
Exit out of Python to clear it's working memory:>>> exit()
Go back to continue:
>>> python3
We can also explicitly import just a specific component, make sure Python has no memory of data_list
>>> data_list
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'data_list' is not defined
>>> mysearch.data_list
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'mysearch' is not defined
>>>
Now, run:
>>> from mysearch import data_list
Found at 10 If you love a flower that lives on a star, it is sweet to look at the sky at nig
Found at 31 For millions of years flowers have been producing thorns. For millions of years
Found at 44 Of course, I love you,' the flower said to him. 'If you were not aware of it, it
Found at 77 If someone loves a flower of which just one exists among all the millions and mi
Found at 81 If you love a flower that lives on a star, then it's good at night, to look up a
None
>>> data_list
['And now here is my secret, a very simple secret: It is only with the heart that one can see rightly; what is essential is invisible to the eye.', 'All grown-ups were once children... but only few of them remember it.', 'People have ...
Because we did not import search function, Python has no knowledge of it
>>> search("something")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'search' is not defined
>>> mysearch.search("something")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'mysearch' is not defined
Let's import both search and data_list:
>>> from mysearch import data_list, search
>>> search("draw")
Found at 79 Will you draw me a sheep?
>>> exit()
Return back to Python:
python3
>>> from mysearch import *
Call your search function and print out data_list
Step5. Create a new module - create a new module, name it to your liking, place a simple variable and a simple function in it, follow same steps above to import it and to use your variable and function
Step6. Run following
>>> import sys
>>> sys.path
You should see something similar to output below, those are the paths where Python will be looking for the libraries
['', '/usr/local/lib/python2.7/site-packages', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python34.zip', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plat-darwin', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages']