-1

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
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Darian Nwankwo
  • 69
  • 1
  • 2
  • 9
  • 1
    Importing (the first time) ***is*** running. – Ignacio Vazquez-Abrams Dec 16 '14 at 08:10
  • What do you mean by *how do I import this file into the python shell without having to run the module itself* ? – Jon Clements Dec 16 '14 at 08:10
  • What do you mean by "import without running the module"? – BrenBarn Dec 16 '14 at 08:10
  • For instance, when I run the shell I can import the math library without having to open the code and run it. Is there a way I can do that with my file? I have done it before when I created a new file in the same directory and imported different files in the same folder, but that was in the Python IDLE. @BrenBarn – Darian Nwankwo Dec 16 '14 at 08:15
  • I'm not certain, but I think your question has to do with the Python path, i.e. where it looks for things when you try to `import`. If this file were in the path, `import classNames` would work everywhere. However, if you could add what you're trying, what you expected to happen and what happened instead that would be helpful. – jonrsharpe Dec 16 '14 at 08:17
  • Can you perhaps provide an example of you importing the file in the way you don't want to do it? – Jon Kiparsky Dec 16 '14 at 08:20
  • What do you expect and what do you see instead? – glglgl Dec 16 '14 at 08:25
  • I type in import classNames.py (which is the name of my file) and i get: Traceback (most recent call last): File "", line 1, in import classNames.py ImportError: No module named classNames.py – Darian Nwankwo Dec 16 '14 at 08:26
  • I expect to see the chevron pop-up next without all the error code. The same result you get when you import a library is what I expect. I tried uploading a picture, but I do not have enough reputation points yet. – Darian Nwankwo Dec 16 '14 at 08:27
  • 1
    Please **edit the question** to include the (code-formatted) error message. The short answer is that Python doesn't know where to look for your file; see e.g. https://docs.python.org/2/tutorial/modules.html#the-module-search-path and http://stackoverflow.com/q/12257747/3001761 – jonrsharpe Dec 16 '14 at 08:30
  • Thank you @jonrsharpe that was exactly what I was trying to do. It worked. – Darian Nwankwo Dec 16 '14 at 08:37

1 Answers1

3

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/.

glglgl
  • 89,107
  • 13
  • 149
  • 217