0

I have looked at this explanation on how to put Python as a system variable for Path. This worked successfully and from my cmd I can now run python which will result in the terminal changing to the >>> notation.

However, I am not able to run the script test.py even when I cd into the right directory. I get the error: name test not defined.

My first question is: how can I solve this and should I solve it?

Digging a bit deeper, I also found that there is the IDLE Python shell that comes with the installation of Python. This interface comes provides support for the >>> notation and you can run Python code in it. My guess that it is better to use this interface to run scripts. However, it is not clear to me how to cd into the right directory and run the file test.py (located in: C:\dev). How should I do run it? Is there a way to cd into a directory/workspace?

So basically, how do I run a python file and where should I run it? cmd or in the shell?

Community
  • 1
  • 1
WJA
  • 6,676
  • 16
  • 85
  • 152

3 Answers3

1

The error sounds like you may be correctly changing directories in cmd, then running python, then entering the name of the file in python. So you are setting a path in cmd, then you ran python, and gave it an object it doesn't know.

Since you aren't importing or running this file from within python you need to tell cmd to run this file in python by changing directory correctly and then telling it to run in python like so:

cd documents/py_scripts

python test.py

I would point you to executable scripts using #!/usr/local/bin/python as your first special line (with your own path to the python interpreter), and using chmod +x file.py command for unix.

Mel
  • 5,837
  • 10
  • 37
  • 42
cornbread
  • 28
  • 7
1

If you are in this for the long haul, you should use env in Python 3.x or virtualenv in Python 2.x. The env setup is included in Python 3.x.

Once an env has been created, the activate script will take care of setting Python in the PATH.

lit
  • 14,456
  • 10
  • 65
  • 119
0

Once inside the interpreter, did you try:

>>> run test.py

The other option would be to not go into the interpreter. Just do:

Path_to_python.exe test.py
tornesi
  • 269
  • 2
  • 8