I'm new to Python. I'm currently on Py3k (Win).
I'm having trouble installing a .py
file. Basically, i want to use the recipes provided at the bottom of this page. So i want to put them inside a .py
and import
them in any of my source codes.
So i copied all the recipes into a recipes.py
file and copied them to C:\Python3k\Lib\site-packages
.
Now, the import
works fine, but when i try to call any function (eg. take
) from it, i got a global name 'islice' is not defined
...So i figured, i should add an itertools
import to recipes.py
I still get the same error? Do i need to change all instances to itertools.<funcname>
? how can i make the import's global? Is this a new Py3k change? Is there something i missing?
Thanks in advance, :)
There are two closely-related issues.
First, within recipes.py, you need access to all of itertools.
At the very least, this means you need
import itertools
at the top. But in this case you would need to qualify all of the itertools functions as itertools.<funcname>
, as you say. (You could also use import itertools as it
and then it.<funcname>
.)
Second, you could do
from itertools import islice, count, chain
but you would need to put all of the needed functions in the list.
Finally, the least recommended, but probably easiest, is to do
from itertools import *
This is dangerous because it pollutes your namespace with everything from itertools
, which is considered bad and uncontrolled, but in this case probably isn't terrible.
Second, you have a similar problem in all of your code that uses recipes.py
; you'll need to qualify or explicitly import everything. So, either
import recipes
recipes.take(...)
or
from recipes import take
take(...)