2

So I have the following api architecture

API >
       -------- data
                    |_ __init__.py
                    |_ data.py
                    |_ data.csv
       -------- model
                    |_ __init__.py
                    |_ model.py
       -------- modules.py
       -------- app.py

model.py & data.py both use modules from modules.py via from modules import * This works fine when launching everything from PyCharm.
However when trying to run it with the shell script in the main directory API

cd data && python data.py && cd ..
cd model && python model.py && cd ..

I get the error: ModuleNotFoundError: No module named 'modules'
How to import correctly modules to run it both on PyCharm and command line?

mobelahcen
  • 414
  • 5
  • 22
  • I think pycharm is running everything from the working directory (I assume `API`), what if you run `python data/data.py` and `python model/model.py`? – mucio Aug 27 '19 at 10:18
  • @mucio same issue – mobelahcen Aug 27 '19 at 10:27
  • 1
    the [Importing files from different folder](https://stackoverflow.com/questions/4383571/importing-files-from-different-folder) can help you – mucio Aug 27 '19 at 10:37

3 Answers3

0

Add this code to data.py and model.py when you are running them separately it will execute this code to import required modules, in pycharm due to __init__.py files it is imported as a package

 if __name__ == "__main__": 
     from ... import modules

later you can use methods/variables from modules as modules.methods() modules.variable etc.,

RAFIQ
  • 905
  • 3
  • 18
  • 32
  • This is running them as main which is not the issue. it actually doesn't change anything if I import at the beginning or after `if __name__ == "__main__": ` – mobelahcen Aug 27 '19 at 10:38
  • I was giving a common code that can run form pycharm and outside, in case if you can have different data.py files you can directly add "from ... import modules" instead of "from modules import *" – RAFIQ Aug 27 '19 at 11:31
0

Well I just had to add the main directory to the python path on both data.py & model.py with:

import sys
sys.path.append('../')
mobelahcen
  • 414
  • 5
  • 22
0

I am a little late for the party, but I'll post what worked for me for the next user getting here from Google. Actually, one can modify the sys.path variable within each Python script as written by @mobelahcen. But when you have several scripts in huge projects, it is not very convenient.

The idea then, in Linux/Unix, is to modify the environment variable PYTHONPATH eg :

export PYTHONPATH=$PYTHONPATH:/home/user/user_defined_module

One could add it to a file read by the shell at startup, e.g., .bashrc.

In Python, when the statement import user_module is executed, the module is searched inside the library installation default path and the directories listed in PYTHONPATH.

eidal
  • 156
  • 2
  • 8