# Importing parts of Packages

Some programming languages offer the possibility of importing a single function from a library, as in

from x import y

I'm looking to import only one function (GraphCoordinates) from the GraphUtilities package.

The following commands do not work:

<< GraphUtilitiesGraphCoordinates

Needs["GraphUtilities", "GraphCoordinates"]

The former gives an error, while the latter loads the entire GraphUtilities package.

How should I proceed?

-

## 1 Answer

A version of this question has been asked before on StackOverflow. You can look at the answers there for some options. To summarize, Mathematica currently does not natively support such a fine-grained encapsulation, which would certainly be a nice thing to have (IMO).

One simple thing you can do is to load the entire package via

Block[{$ContextPath}, Needs["GraphUtilities"]] (see this answer for an explanation), and then refer to your function by its long name: GraphUtilitiesGraphCoordinates You can then go further and create an alias in your current context, if you don't want to type the long name: GraphCoordinates = GraphUtilitiesGraphCoordinates Since the loaded package is not on the$ContextPath, this will not lead to shadowing. Note however that such aliases will work most of the time, but not always.

This workaround should work fine if you are ok to load the entire package and just don't want all its public functions to become public in your current context ("Global",or whatever it is). If you truly want to avoid even loading the entire package, you are (currently) out of luck.

-
Bleh! This is what I feared... Since I found nothing in the official documentation. Thanks for the hack! –  CHM Aug 10 '12 at 19:56