1

I have a python file that I wrote myself, that has several functions in it. I am trying to import it from the python interpreter from within powershell (Windows 7). This is a learning exercise. But it isn't importing, it's throwing an error that the module does not exist. My question is where do I need to save my .py file to? Is there a specific place that python looks for modules to be imported? I'm using python2.7.10.

This isn't a module from the standard library or a third party that I've downloaded. It is a python file that I am importing so that I can call functions from it...

It's answered now, I was just calling python from withion the wrong directory. I have to call python from the same directory that the file is saved in. Thank you guys.

Sinux1
  • 93
  • 5
  • 11
  • Can you clarify how you're importing it from within powershell? We've had a lot of questions here lately that are purely about python but are tagged with powershell, and they all seem to be from people who are just learning. Is there a specific tutorial or guide you're following? I'm not trying to pick on you, just trying to figure out this influx. – briantist Aug 30 '15 at 22:58
  • 1
    Try: [Importing classes from another directory - Python](http://stackoverflow.com/q/29447635/55075) – kenorb Aug 30 '15 at 23:01
  • I'm following the Learn Python the Hard Way book, and I am in powershell. I type `python` to open my interpreter, and it opens correctly, then at the the prompt (`>>>`) I type `import ex25` (ex25.py being the file name) and it throws an error. – Sinux1 Aug 30 '15 at 23:05
  • @SinuxTine The module name isn't the same as filename. Each module needs to be in separate directory and it should contain `__init__.py` empty file and the folder should be within your `PYTHONPATH`. Please check the linked post for more details. – kenorb Aug 30 '15 at 23:07
  • @SinuxTine I see. In that case you are using powershell as a shell, only for the purpose of launching the python the interpreter, the way you might launch it from the command prompt or windows explorer. So I've removed the powershell tag for that reason (it's not relevant to your issue). Do check the link provided by kenorb, it seems that your solution is there. – briantist Aug 30 '15 at 23:11

1 Answers1

3

Save your module.py file to /documents. Then navigate to /documents in Powershell and start the Python command line by typing python and pressing enter. Then you can import the file with the command import module

gtlambert
  • 11,711
  • 2
  • 30
  • 48
  • 1
    Thank you... works perfectly now. I completely forgot that where I open python at counts... It's been a couple weeks since practicing... – Sinux1 Aug 30 '15 at 23:08