This works:
system "python \"C:/Program Files (x86)/Google/google_appengine/someScript.py\"
This doesn't work:
system "python \"someScript.py\"
The directory containing someScript.py is added to my windows PATH variable, so if I open a CMD window and type:
someScript.py
it runs fine.
How do I make is so that my ruby script is able to call someScript.py while observing the windows environment path variable so I don't have to specify an absolute path for the python script?
Python does not use the $PATH environment variable.
You always have to specify a full path to the python script on the command line. This can be relative from the current directory or an absolute path.
The association between files with the .py extension and the Python executable is a separate registration, where Windows executes the Python executable after the script file has been located on the current search path. Windows passes the full, absolute path of the script to the Python executable when it invokes it. When you execute the Python executable directly using system you bypass that lookup.
You could try to execute the Python script directly using system "someScript.py" without the python executable, but I am not familiar enough with Ruby and Windows to know if that'll honour the .py extension registration used by the Windows command prompt.
Related
I have Python 3.6 and Windows 7.
I am able to successfully start the python interpreter in interactive mode, which I have confirmed by going to cmd, and typing in python, so my computer knows how to find the interpreter.
I am confused however, as to how to access files from the interpreter. For example, I have a file called test.py (yes, I made sure the correct file extension was used).
However, I do not know how to access test.py from the interpreter. Let us say for the sake of argument that the test.py file has been stored in C:\ How then would I access test.py from the interpreter?
The simplest way would be to just do the following in cmd:
C:\path\to\file\test.py
Windows recognizes the file extension and runs it with Python.
Or you can change the directory to where the Python program/script is by using the cd command in the command prompt:
cd C:\path\to\file
Start Python in the terminal and import the script using the import function:
import test
You do not have to specify the .py file extension. The script will only run once per process so you'll need to use the reload function to run it after it's first import.
You can make python run the script from a specific directory:
python C:\path\to\file\test.py
In command prompt you need to navigate to the file location. In your case it is in C:\ drive, so type:
cd C:\
and then proceed to run your program:
python test.py
or you could do it in one line:
python C:\test.py
I may be misunderstanding what you are seeking, but I think what you are asking for is a IDE-like environment where you can load Python scripts, edit, and debug.
You already have IDLE, https://docs.python.org/3.6/library/idle.html, which came with the Python installation.
There are many IDEs available for Python. https://wiki.python.org/moin/IntegratedDevelopmentEnvironments Personally, I like using PyDev on Eclipse.
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?
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.
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").
Hello I am attempting to learn Python using django in powershell on windows to create a simple CRUD app but have some questions regarding the setup and usage.
I have setup the path environment variable to include paths to a folder that includes test .py scripts that I thought allowed me to run directly from a c:\ in powershell but when I try to run the samples using:
c:\> python test1.py
c:\> python .\test1.py
It does not work and I get:
c:\Python27\python.exe: can't open file '.\test1.py':[Errno 2] No such file or directory
but if I CD to path first and run it works fine and I get the output in the powershell window itself:
c:\> cd c:\Python27\Scripts
c:\Python27\Scripts> python test1.py
In addition if I just type the name of the script from the root c:\ prompt such as:
c:\> c:\Python27\Scripts\test1.py
c:\> test1.py
it runs the script by opening in a dos box that that closes immediately since it ran the script and finished.
What is the difference between these methods and is there an issue running scripts one way or the other?
The issue here is that in the first case:
c:\> python test1.py
c:\> python .\test1.py
You're running python itself, and then the file is an argument to the interpreter. So python will be affected by PATH, but the file is relative. Both of these file paths mean to look for it in the same directory, which is the root of C:. Since the file isn't located there, the error results.
The second case:
c:\> c:\Python27\Scripts\test1.py
c:\> test1.py
It's the file itself being run as an "executable", factoring in PATH. On Unix, this would use the shebang and invoke the shell in pretty much the same way, however that's not the case on Windows. Instead, it's basically running whatever is associated with the .py extension at the OS level, which is likely to invoke a new Python interpreter in a new Window, hence the behavior where it disappears after it's done.
On Windows, since there's no shebang, I generally prefer to run the python interpreter directly and pass the script as an argument. I would generally switch to the directory the script is located in first. This way it's more shell-centric, and, I think, straightforward.
You can run pushd . to save the cwd, and then after popd to go back, optionally. That would work in .cmd files and similar, too.
Are you sure test1.py is in C: and not in c:\Python27\Scripts
Say I want to run an executable with a filename argument that is within my working directory, in Windows cmd I would go:
C:\Python27\python signalme.py
How can I do so in Cygwin? NOTE: C:\Python27\python is an executable. Please give me a full answer, I read somewhere here that you should add a .\a, but I couldn't know where to add it.
Cygwin attempts to make a Linux-like user environment available on Windows. To run a executable file on a Linux-like command line, you need the following things:
You have to be able to find the executable, which means one of the following:
The executable is in a directory that is included in your PATH environment variable.
You know the explicit absolute path to the executable, which you can specify at the command line
You know the relative path from the current directory to the executable, which you can specify at the command line.
The file has to have the executable permission set for the user you're attempting to use to run that file.
So, to run an executable in your working directory, you can specify the relative path to the working directory, along with the filename: ./foo.exe.
In your case, you want to use the Python interpreter to run a local Python file. You will most likely need to use the Cygwin-installed Python. That Python will probably be in your PATH, so just run:
python signalme.py
This could fail, with an error message like bash: python: command not found - in which case you should re-run your Cygwin setup, look for the opportunity to install Python, and make sure you install it.
/cygdrive/c/python27/python.exe signalme.py
I have always used a mac to write and run python scripts. However, I have a bunch of files on a PC that I need to run a script on. I have never really used a PC and don't know how to run the script.
If I go to the command program on the PC and type in python, nothing happens. How do I find where the python path is in order to get into the python prompt? Also, once I am in the prompt, is the importing of modules the same as in a Unix system?
Python isn't added to the system environment's PATH variable by default on windows. You have to either add the path to the directory containing the Python.exe file to the PATH variable, or call python explicitly.
This issue has been addressed in the Python documentation:
Python Documentation: # How to run a Python program under windows
Assuming Python is installed, it is usually placed in a folder prefixed with "Python" and the major/minor version. E.g. C:\Python26