2

I'm a Python beginner and have just started using packages.

When you're calling a function after you've imported the package, do you always have to reference it to make it work, or is that just good practice?

For example, I'm working with the pandas package in ipython.

import pandas
import numpy as np

df = pandas.read_csv('/Users/admin/Documents/data.csv')

Do I always have to use the package name to call the function? If I don't, is it a best pract to always reference it?

mikebmassey
  • 8,354
  • 26
  • 70
  • 95
  • you could also do ´from pandas import read_csv´ and then you can call read_csv without having to write "pandas" again. – demux Jun 11 '12 at 20:16

5 Answers5

7

If you adjust your import statement, then you don't need the package name.

For example:

from pandas import read_csv
import numpy as np

df = read_csv('/Users/admin/Documents/data.csv')

See this related question: Importing modules in Python - best practice

Community
  • 1
  • 1
Tim Henigan
  • 60,452
  • 11
  • 85
  • 78
2

No, you don't have to; you can use the from [package] import [symbol] form, which will import the [symbol] in to the local namespace. That said, unless your app is heavily object oriented (as opposed to being module-centric), more often then not, it becomes both dangerous (accidentally reusing imported names locally) and confusing (hard to figure out where something is coming from and what it is intended to do) when you don't use package names.

Silas Ray
  • 25,682
  • 5
  • 48
  • 63
1

If you have a local reference to an object, you can use that reference. If you do not, then you must reference the module first, then access it for the object.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

The import statement in python has a few different forms. You could do this to only import the read_csv function:

from pandas import read_csv

df = read_csv('/path/to/file')

or this to import everything in the pandas module, so that you don't have to prefix it with pandas.:

from pandas import *

df = read_csv('/path/to/file')

This method is usually discouraged, though, because it makes it difficult to see where functions/classes/variables came from.

see http://docs.python.org/tutorial/modules.html for more info

hdgarrood
  • 2,141
  • 16
  • 23
0

If you change your import statement to

from pandas import *

you could then reference the function directly:

df = read_csv('/Users/admin/Documents/data.csv')

This will bring everything from the imported package into your current namespace. You can also specify piece-wise what should be imported. As discussed in the comments below, there are pros and cons to both approaches.

In my opinion, use whichever approach is least confusing in your current context. :)

dckrooney
  • 3,041
  • 3
  • 22
  • 28
  • 3
    Don't ever do this though. It's very bad practice. – deadly Jun 11 '12 at 20:17
  • 1
    To elaborate, when using `from package import symbol` it is a much better idea to import just the symbols you need, to avoid pulling things into your namespace you didn't intend to. – Russell Borogove Jun 11 '12 at 20:18
  • @deadly Why is `import *` bad practice? Overwriting `namespaces`? – mikebmassey Jun 11 '12 at 20:19
  • I *think* he's saying it is bad practice to import `*` specifically. – Silas Ray Jun 11 '12 at 20:19
  • @mikebmassey Partly, as Russell says, but also because it makes tracking down bugs absolute hell as you have no obvious link between a function and the module it was imported from. – deadly Jun 11 '12 at 20:21
  • @deadly, From what I recall, there are some packages designed specifically to be star-imported. As with most things in Python, saying 'never' should be avoided, and 'it is strongly discouraged and probably not what you need' should be substituted. – Silas Ray Jun 11 '12 at 20:22
  • @sr2222 Packages shouldn't be make like that. My life is made many times harder by star-imports. – deadly Jun 11 '12 at 20:23
  • @deadly, And yet some are, including, if memory serves, some in widely used packages. – Silas Ray Jun 11 '12 at 20:23
  • @sr2222 Might doesn't make right. They still deserve a good slapping. :P What packages, by the way? I don't think I've come across any. – deadly Jun 11 '12 at 20:25
  • @deadly Can't recall specifically at the moment. I just remember coming across some comments in the past. My point was though that in Python, almost nothing can be spoken of in absolutes. :) – Silas Ray Jun 11 '12 at 20:27
  • BTW it's bad practice because it can lead to name collisions. You have a variable called `foo`, now you say `from pandas import *`, and in pandas there is a function called `foo`. Which will win? You might feel safe if you think you would find this kind of bug while you develop, but it is always possible that symbols get added in standard modules and will trigger this bug in your script any time the standard library is updated. So just don't do it (the star import, not the update). – Ulf Rompe Jun 11 '12 at 20:28
  • @UlfRompe Exactly. That's why it is strongly discouraged. I've just experienced people that held religiously to things like "never import star", and it made things unnecessarily difficult. It should be, "you need a damn good reason to import star." – Silas Ray Jun 11 '12 at 20:29
  • @sr2222 Sure, but don't tell the beginners that! :D – deadly Jun 11 '12 at 20:31
  • @deadly The problem is that beginners who are taught absolutes frequently become professionals (and worse, managers) who go around with lots of "thou shalt nots". And do code reviews. – Silas Ray Jun 11 '12 at 20:33
  • 1
    @deadly, sr2222: I think this discussion is exactly what beginners should see. It gives a much deeper understanding of the issue than a simple "Never"/"Always" answer. – dckrooney Jun 11 '12 at 21:14