1

Really frustrated with this as one thing works at one time. Sometimes import filename.py works. But in the tutorials all I see is python filename.py. But when I try to give that, I am facing an error like invalid syntax.

I have edited all the environment variables and I have C:\Python27 folder in the same location. To be able to run the files using python filename.py what are the conditions that must be met? What should be the current working directory? Should the .py files be there in the same working directory?

5
  • 1
    It completely depends on your program. You should be able to run python /path/to/your/program.py to run it. But imports might be in specific places that need certain paths so you'll need to set that up. Your question is too broad though. Narrow down a specific issue you're having with a specific script and post that. Commented May 12, 2015 at 17:20
  • It sounds like there is an issue with your python file filename.py. Can you include its contents? It sounds like your environments is okay. To be sure, what is your output when you run 1) python? 2) python filename.py? Commented May 12, 2015 at 17:21
  • @Ezra I am getting same error(invalid syntax) when I give python ? and python filename.py Commented May 12, 2015 at 17:34
  • You're not putting python filename.py inside a .py file are you? Just checking... Commented May 12, 2015 at 18:05
  • @JeffBridgman I think thats what I was doing. I mixed up the normal command prompt and python command shell that comes with the installation. Having two options to run the .py files confused me. Now, thanks to Etan Reisner, I've sorted things out. I was such an idiot all this time!! Commented May 12, 2015 at 18:08

2 Answers 2

6

import name is a python keyword for use within a python script and/or the python interactive interpreter (repl).

python filename.py is a shell command for use at a command prompt or within a shell script to run the python interpreter on a given python script file.

The working directory does not matter other than for whether the file listed in python filename.py can be found.

So for python filename.py to work you must be in the same directory as filename.py but you could just as readily use python c:\users\user\some\other\path\filename.py in which case the current directory isn't used to find the file.

If you get python syntax errors from attempting to run python on a python file that's a python error in the code and you will need to look at the python file to see what the error is.

Sign up to request clarification or add additional context in comments.

1 Comment

OMG!! Made a hell lot of sense when you put it that way. I got much more clarity now.
0

Just to be clear, typing python filename.py only works from the Terminal (i.e. cmd.exe, Windows PowerShell, the "Terminal" application on a Linux kernel, etc.), not from the Python interpreter (i.e. python.exe), and only works if you have used the cd command to change into the directory in which the file is saved (so that the terminal knows where to look for filename.py). import filename can be used from the Python interpreter, but is not the ideal method as it creates a compiled version of filename.py and can only be used once (you would have to restart the interpreter to do so again). I'm not sure whether this works in the official Python distribution available from the Python website, but at least in the Anaconda distribution, you can run a file from the Python interpreter using runfile("C:/Users/CurrentUser/Subfolder/filename.py").

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.