1

I've been developing a project in python for some time using pycharm. I have no problem running it in pycharm, but I want to try and running it from the command line (I'm using windows). When I try to run my main file using python <filename> from within the root directory, I get a module not found error. What is pycharm doing/how can i replicate it? Also, pycharm creates pycache folders. If my understanding is correct its compiling the files, and that makes the runtime faster. Since my runtime is already long i'd like to do the same.

I'm using python 3.6

edit

File Structure

    -Root\
    ----scheduler\
        ------main.py
        ------matrices
        ------models
        ------rhc
        ------Singleton.py
        ------utils.py
        ------__init__.py
        ------apis\
                acedb.py
                metrics.py
                __init__.py
        ------matrices\
                distancematrix.py
                __init__.py
        ------models\
                branch.py
                constants.py
                customer.py
                dispatch.py
                technician.py
                workorder.py
                __init__.py
        ------rhc\
                pathfinder.py
                rhc.py
                schedule.py
                sched_time.py
                tech_schedule.py
                __init__.py

Edit 2

Found the solution, i had to move my main files outside of the modules Thanks!

  • 1
    pycache is created by the interpreter, not the IDE. It contains bytecode. It's not exactly compiled, and it doesn't really affect the runtime (it always runs the bytecode, anyway). – kraskevich Apr 05 '17 at 19:02
  • You are getting the error probably because of the way that PyCharm structures your project. Could you post a chart that shows the directory structure of all of the files that you have for your program and also the error message that you get? – gr1zzly be4r Apr 05 '17 at 19:07
  • 1
    Make sure that the python executable run with 'python' on the command line is the same as the python used by PyCharm. Run `import sys; print(sys.executable)` with each. If they are different, you need to duplicate the needed modules in the `site-modules` for each. – Terry Jan Reedy Apr 05 '17 at 19:19
  • @gr1zzlybe4r i posted the file structure in an edit just now and im getting a ModuleNotFoundErro –  Apr 05 '17 at 19:37
  • @TerryJanReedy just checked and they are not different –  Apr 05 '17 at 19:37

2 Answers2

1

If you have the below folder structure for instance. add an empty file named init.py and import from your app.py, in case you have a Module1Class , you can always import it like this

from modules.module1 import Module1Class
from modules.module2 import Module2Class

Folder structure

/app.py
/modules
    /module1.py
    /module2.py
    /__init__.py 

The first time you rum app.py from terminal like python app.py, the .pyc files will be created, leaving you with the following folder structure

/app.py
/modules
    /module1.py
    /module1.pyc
    /module2.py
    /module2.pyc
    /__init__.py 

Please refer to the python documentation as it's very well documented on how to create modules and importing them. I'm more used to python2.7 , so my answer might not be an exact fit to newer python versions.

https://docs.python.org/3/tutorial/modules.html

From there you can learn more about __ init __.py and module creation , exporting and importing

PS: I use only text editors to develop in python, as I find pycharm a bit on the heavy side, so I cannot explain how exactly pycharm works behind the curtains.

Ilhicas
  • 1,429
  • 1
  • 19
  • 26
  • I have these __init__.py files in all modules already (just posted file structure). –  Apr 05 '17 at 19:38
0

see ModuleNotFoundError: What does it mean __main__ is not a package? for a good example of ModuleNotFoundError description (was answered fast)

I bet that pycharm is configured to use a different python interpreter of virtual environment.

Community
  • 1
  • 1
Serge
  • 3,387
  • 3
  • 16
  • 34