简体   繁体   中英

execute a python file from pycharm console

I have a python file and I want to execute it from PyCharm console.

I'm in the directory of the project (where there is my file)

I tried to follow the solution in this thread: Running a module from the pycharm console

but I got the following error:

from project_main_folder import *
home_scorer_macro.py
Traceback (most recent call last):
  File "C:\Python35\lib\site-packages\IPython\core\interactiveshell.py", line 2963, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-20-24aaf6f313c6>", line 1, in <module>
    home_scorer_macro.py
NameError: name 'home_scorer_macro' is not defined

I tried directly without "import" to call execute the file, like:

python home_scorer_macro.py

but I got the following error as well:

File "<ipython-input-29-735c29c27d57>", line 1
    python home_scorer_macro.py
                           ^
SyntaxError: invalid syntax

You are confusing the console for the command prompt. The console allows you to type code and have it interpreted as you type line by line (or function by function). The command prompt allows you to say things like python home_scorer_macro.py . If you are trying to run the home_scorer_macro function from learning you can try:

from learning import home_scorer_macro
home_scorer_macro()

It is difficult to tell what you are trying to do without you providing any description of what you are trying to accomplish, though. You could also try something like

import os
os.system('"python home_scorer_macro.py"')
raw_input()

if you would like to combine commmand prompts and consoles. The os.system lets you run commands from within your python script

As well you can go to the Terminal tab and run python home_scorer_macro.py

PyCharm 选项卡

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