5

I have a project structured this way...

main.py imports scripts from subfolders like so:

from controllers.available_balances_controller import available_balances_controller

Subfolders:

  • models
  • views
  • controllers

When running main.py in Pycharm it works find.

When I try to run in terminal I get import errors:

Traceback (most recent call last):
  File "main.py", line 6, in <module>
    from controllers.available_balances_controller import available_balances_controller
ImportError: No module named controllers.available_balances_controller

Am I importing the scripts wrong in main.py?

What is the proper way to do the importing?

Emily
  • 2,129
  • 3
  • 18
  • 43
  • Any chance your terminal is using a different Python than what you configured for PyCharm? Or, perhaps you are not running the terminal Python from the project directory? – Paul Everitt Apr 28 '16 at 10:58
  • I'm using python3 terminal command + script name in the project directory. Pycharm is configured for 3.5. When I added blank __init__.py to all the folders it found the project modules. But now it's not finding the site packages I imported in Pycharms interpreter. Is there someplace I need to specify these in __init__.py? – Emily Apr 28 '16 at 11:10

1 Answers1

3

Try running your script with the -m flag:

$ python -m main

That means that you are running your main.py as a module inside a python package, not as a simple script. PyCharm makes it easy for you by assuming so when you create a project. When you are in the terminal, you need to specify it yourself. You don't need __init__.py files inside your directories in Python3.

Check out:

Community
  • 1
  • 1
cyberbikepunk
  • 1,302
  • 11
  • 14