简体   繁体   中英

How to run a python program like pycharm does

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!

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM