How do you import files into the Python shell?
Question: I made a sample file that uses a class called `Names`. It has its
initialization function and a few methods. When I create an instance of the
class it retrieves the instance's first name and last name. The other methods
greet the instance and say a departure message. My question is: how do I
import this file into the Python shell without having to run the module
itself?

The name of my file is classNames.py and the location is
C:\Users\Darian\Desktop\Python_Programs\Experimenting

Here is what my code looks like:

    
    
     class Names(object):
         #first function called when creating an instance of the class
         def __init__(self, first_name, last_name):
             self.first_name = first_name
             self.last_name = last_name
    
          #class method that greets the instance of the class.
          def intro(self):
              print "Hello {} {}!".format(self.first_name, self.last_name)
    
          def departure(self):
              print "Goodbye {} {}!".format(self.first_name, self.last_name)
    

But I get the error:

    
    
    Traceback (most recent call last): 
      File "<pyshell#0>", line 1, in <module> 
        import classNames.py 
    ImportError: No module named classNames.py
    


Answer: I am not clear about what you expect and what you see instead, but the
handling of your module works exactly like the `math` and any other modules:

You just import them. If this happens for the first time, the file is taken
and executed. Everything what is left in its namespace after running it is
available to you from outside.

If your code only contains just `def`, `class` statements and assignments, wou
won't notice that anything happens, because, well, nothing "really" happens at
this time. But you have the classes, functions and other names available for
use.

However, if you have `print` statements at top level, you'll see that it is
indeed executed.

If you have this file anywhere in your Python path (be it explicitly or
because it is in the current working directory), you can use it like

    
    
    import classNames
    

and use its contents such as

    
    
    n = classNames.Names("John", "Doe")
    

or you do

    
    
    from classNames import Names
    n = Names("John", "Doe")
    

**Don't do`import classNames.py`**, as this would try to import module `py.py`
from the package `classNames/`.

