1

I have the following structure:

/MyApp
   /Module_A
   __init__.py
   serviceFile.py
   /Module_B
   clientFile.py

I am trying to call a function in ServiceFile.py from ClientFile.py

From MyApp.Module_a import ServiceFile

but I am getting the error

ImportError: No module named MyApp.Module_a

Why? BTW, When I run this from pyCharm it works fine only in CDM. Both uses python 2.7

Eran Witkon
  • 4,042
  • 4
  • 19
  • 20
  • The reason this work in Pycharm is because it does some behind the scenes magic which makes sure Python is able to locate `Module_a`. If you want to run the from a terminal, you're probably going to have to mess with `sys.insert()`. – Christian Dean Jul 07 '17 at 02:11
  • 2
    You have a lot of discrepancies in your file listing, question text, python code and python error relating to capitalization. Python is case-sensitive. Make sure all of those `Module_A` and `Module_a`, `serviceFile` and `ServiceFile` pairs are the same everywhere. – robert_x44 Jul 07 '17 at 02:13
  • Is `MyApp` and `Module_B` missing an `__init__.py`? – cs95 Jul 07 '17 at 02:17

2 Answers2

2

There's potentially 2 sources of error. The first, like @TomCho has already pointed out is if the __init__.py file is not properly in your modules. However, since it works in PyCharm, I'm guessing it's the second.

The second is if your PYTHONPATH variable is not set up properly. There are a number of places which python will by default look for modules. This includes places like the Python install directory and the current working directory. To view a full list of places which Python looks, do..

import sys
print(sys.path)

Python will then descend into each directory that has a __init__.py file, which is what makes that file so important. However, most likely you are not putting your code into the default places Python looks. And that's the whole purpose of the PYTHONPATH environment variable. You make it a list of paths where your packages are, and then those will be added to the sys.path seen above.

By default, PyCharm will add your project directory to the PYTHONPATH, which is why I'm guessing it worked in PyCharm, but not on the command line.

To set PYTHONPATH in Linux, go to the command line and type

export PYTHONPATH=/path/to/python/module

If you have more than one path you need to add, you can keep appending with : so the more general case is

export PYTHONPATH=$PYTHONPATH:/path/to/python/module

And you can keep doing that. I typically add that line to my ~/.bashrc file.

If you are in windows, modifying environment variables is different, but it's still pretty easy.

nanotek
  • 2,959
  • 15
  • 25
1

You should put a __init__.py file in both MyApp directory and Module_B. That makes the call from MyApp.Module_B import clientFile work for me. Here's my tree:

└── MyApp
    ├── __init__.py
    ├── Module_B
    │   ├── clientFile.py
    │   ├── __init__.py

By default Python won't know a certain .py file is part of your module if it isn't "linked" to an __init__.py file.

TomCho
  • 3,204
  • 6
  • 32
  • 83