简体   繁体   English

如何像pycharm一样运行python程序

[英]How to run a python program like pycharm does

I've been developing a project in python for some time using pycharm. 我一直在使用pycharm在python中开发项目。 I have no problem running it in pycharm, but I want to try and running it from the command line (I'm using windows). 我在pycharm中运行它没有问题,但是我想尝试从命令行运行它(我正在使用Windows)。 When I try to run my main file using python <filename> from within the root directory, I get a module not found error. 当我尝试从根目录中使用python <filename>运行我的主文件时,出现模块未找到错误。 What is pycharm doing/how can i replicate it? pycharm在做什么/我该如何复制它? Also, pycharm creates pycache folders. 另外,pycharm创建pycache文件夹。 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 我正在使用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 编辑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 添加一个名为init .py的空文件,并从app.py导入,如果您有Module1Class,则始终可以像这样导入

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 第一次从终端像python app.py一样浏览app.py时,将创建.pyc文件,从而使您拥有以下文件夹结构

/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. 请参考python文档,因为它在如何创建模块和导入模块方面的文档非常详尽。 I'm more used to python2.7 , so my answer might not be an exact fit to newer python versions. 我更习惯python2.7,所以我的答案可能不完全适合较新的python版本。

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

From there you can learn more about __ init __.py and module creation , exporting and importing 从那里您可以了解有关__ init __.py以及模块创建,导出和导入的更多信息。

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. PS:我只使用文本编辑器来开发python,因为我发现pycharm有点沉重,所以我无法解释pycharm究竟是如何工作的。

see ModuleNotFoundError: What does it mean __main__ is not a package? 请参见ModuleNotFoundError:__main__不是软件包是什么意思? for a good example of ModuleNotFoundError description (was answered fast) 一个关于ModuleNotFoundError描述的好例子(被快速回答)

I bet that pycharm is configured to use a different python interpreter of virtual environment. 我敢打赌pycharm配置为使用虚拟环境的其他python解释器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PyCharm 如何运行 Python 脚本? - How does PyCharm run Python scripts? 如何在 PyCharm 中运行与 distutils 打包的 python 程序? - How to run a python program packaged with distutils from source in PyCharm? 在PyCharm中使用控制台中的命令行运行python程序 - Run python program in PyCharm with command line in console 如何在pycharm的运行工具windosw上运行python程序,而不是python控制台? - How to run python program on pycharm's Run tool windosw, instead of python console? PyCharm 是否具有在部分/单元格中执行 python 脚本的功能(如 VsCode - 在交互窗口中运行)? - Does PyCharm have the feature for executing python script in parts/cells (like in VsCode - Run in interacting Window)? 尝试在 Pycharm 中运行程序 - Trying to run a program in Pycharm 如何使用运行/调试配置中的参数字段将所有文件(具有给定名称模式)传递给 PyCharm 中的 python 程序? - How to pass all files (with given name patterns) to python program in PyCharm using the parameters field in run/debug configurations? Pycharm:如何使用程序打开 Python 控制台 - Pycharm: How to open a Python console with a program 如何从PyCharm导出一个Python程序 - How to export a Python program from PyCharm 在PyCharm中运行Python程序后,Tkinter窗口会自动关闭 - Tkinter window closes automatically after Python program has run in PyCharm
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM