iTamizhan
iTamizhan

Reputation: 29

Syntax error when trying to run a Python file

File ex.py:

print('hello,word')

Trial run:

>>> d:\learning\python\ex.py

SyntaxError: invalid syntax

Upvotes: 0

Views: 2892

Answers (2)

Open AI - Opting Out
Open AI - Opting Out

Reputation: 24133

You need to import the module:

>>> import ex

See the documentation about modules

The module has to be on your path or in the current working directory. You can change directories using chdir:

>>> import os
>>> os.chdir(r'd:\learning\python')

>>> import ex
hello,word

Or, in the command prompt, you can set PYTHONPATH=d:\learning\python, for example, before you start python, and d:\learning\python will get added to sys.path.

Instead, you could add it programatically:

>>> import sys
>>> sys.path.insert(0, r'd:\learning\python')

>>> import ex
hello,word

Upvotes: 2

Nayuki
Nayuki

Reputation: 18533

On the command line, outside the Python console, type:

python D:\learning\python\ex.py

Upvotes: 3

Related Questions