Running Python on Ubuntu Shell - python

I am trying to run python on Ubuntu. ADDED: It's a double boot system with Windows.
If I type python on the shell, it opens python. But I want to run a python file.
I have my python (.py) file saved on windows Desktop.
On Windows when I run a .py file in my command prompt, I just have to change directory to Desktop then type python myfile.py and the code runs successfully.
When I try to do that same thing in Ubuntu, it does not work. I made a Desktop directory in Ubuntu using the mkdir function.
Now, when I type python myfile.py, I get the error:
python: can't open file 'myfile.py': [Errno 2] No such file or
directory
I tried typing python on the Ubuntu shell then dragging the myfile.py file (~$ python C:\Users\username\Desktop\myfile.py), it used to run and then close the answer right away, but now I would get the error
python: can't open file 'C:UsersusernameDesktopmyfile.py': [Errno 2]
No such file or directory
Can anyone tell me what are the exact steps I need to do to fix this?
EDIT:
Here is what I am writing from the answers below:
~$ python /home/username/Desktop/myfile.py
Yet, I am getting this error:
python: can't open file '/home/username/Desktop/myfile.py': [Errno 2] No such file or directory
EDIT 2**
So here is something new:
if I write
python /home/username/Desktop myfile.py
I get this error
/usr/bin/python: can't find 'main' module in '/home/username/Desktop
If I write
python /home/username/myfile.py
I do not get an error, but I do not get any output either. Ubuntu just goes to the next line $

Strange on Ubuntu your path starts with c:\...
On Ubuntu user folders are usually in /home, user folder can be referenced by ~, so IMHO python ~/Desktop/myfile.py should work in your environment.
EDIT: noted you made Desktop folder, not original Desktop, that way when you are in that folder type pwd it will show full path, then put it in python PATH/myfile.py (and just in case you may type ls to show list of files in current folder on Linux to check you are indeed in the right folder where your program is).
ADDED: after discussion it turned to be double boot system, mount showed mounted windows disk and file.py was found and run!

Backslash '\' is an escape character, in Unix it will not be used if you put it in a path. It results in your error of path not found.
Use slash '/', your code should run.
It is weird that you have a path for windows, in Unix you should not have this type of path...
Open a terminal, go to the folder with your python script. Use pwd in the terminal to know the exact location and then copy the path and use the following (I take an example here):
python PATHTOYOURPYTHONSCRIPT/mypythonscript.py

You're trying to use windows path representation in Linux. Windows and Linux has different path representations.
In windows, you can use C:\ but in Linux it's just a / which is used to denote the root directory.
In the terminal, type 'pwd' where your python file is present in Ubuntu and you'll see the output as '/home/username/Desktop' which is not like windows.
So you need to run like, 'python /home/username/Desktop/my file.py'.
If you need to access the file present in the windows partition, you need to mount the windows partition. This can be done using the Files app present in Ubuntu. After that, you can navigate to /mnt/media/ and find your file.

Related

File runs from python.exe

I just switch laptops and everything seems to be normal except this. When I open CMD and try to run a file like py filename.py
it comes up with an error
C:\Users\john\AppData\Local\Programs\Python\Python37\python.exe: can't open file 'filename.py': [Errno 2] No such file or directory
It defaults to the python.exe file. It does it for every file that I try to run as well. I am in the correct directory (the directory where the actual file is in) but it keeps defaulting to that error. I am running windows 10 with python 3.7.3.
You may have to change the default directory where your terminal looks for the python.exe file and its packages. You must change both. One variable points to the python[version] folder and the other points to the python[version]\scripts folder.
This page shows you how to redirect your terminal in windows:
https://superuser.com/questions/1399544/how-to-change-default-python-executable-on-windows-10/1399546
Another tip is that you must change these two identical directories for both the "user variables" and the "system variables" listed in the section below it. I believe the Python installation has probs with this, especially if it's a reinstall or a failed update. And on Windows in general.
Best,

Running Shell Script from Applescript

I want to run a Shell script using Automator in OSX. The shell script calls a Python file which is located in the folder "/Users/Tex/Python". My code is:
on run {input, parameters}
tell application "Terminal"
do shell script "/Users/Tex/anaconda3/bin/python /users/Tex/Python/MoveFiles.py $#"
end tell
return input
When I execute the automator script I get an error as follows:
Terminal got an error: /users/Tex/anaconda3/bin/python:
can't open file '/users/Tex//Python/Move_Files.py': [Errno 2] No such file or directory
I suspect that the problem is that the "/Users/Tex" folder is located on a different drive to the OSX operating system but that the Shell interpreter is not aware of this. I have tried using the full path to the python file, i.e. including the volume name (iMac Disk A) however the fact that this has spaces in it seems to create yet another issue for the Shell.
Appreciate feedback on how to make thsi work!

Why don't I have to add 'python' before invoking Python script?

I am new to programming, and Python is my first language.
I've added Python to my Path, but when I use the Command Prompt, I don't have to add python before myscript.py as opposed to many tutorials I've seen. Here is an example:
C:\User\MyName>Welcome.py
Welcome to Python
Python is fun
When I enter 'python', there is a subsequent error:
C:\User\MyName>python Welcome.py
python: can't open file 'Welcome.py': [Errno 2] No such file or directory
Do I really need the 'python'?
Thanks in advance!
If you followed the Python on Windows FAQ, it seems that the standard Python installer has already taken the liberty of associating .py files with an open command to ..\..\Python\python.exe "%1" %*.
How do I make Python scripts executable?
On Windows, the standard Python installer already associates the .py extension with a file type (Python.File) and gives that file type an open command that runs the interpreter (D:\Program Files\Python\python.exe "%1" %*). This is enough to make scripts executable from the command prompt as ‘foo.py’. If you’d rather be able to execute the script by simple typing ‘foo’ with no extension you need to add .py to the PATHEXT environment variable.
Who'd have thunk! This isn't the way it used to be four years ago when I first installed Python on my Windows machine.
Yes and no.
It really depends on how the script is written.
On most unix systems (Linux, Mac OS), you could include #!/bin/python to the top of (as the first line of) your script and therefore execute it by just calling the filename on the command line. That first line tells the shell that this file contains a python program. The shell then uses the python interpreter to execute the file (translation: it translates your $ Welcome.py to $ /bin/python Welcome.py <- note that python is being called explicitly and that it's the same path as what's on the first line of your file).
Presumably, the Windows OS can also be instructed in the same way, though I have never been able to do it myself, nor have I tried very hard (I moved away from windows about 5 years ago). This is why you'll need to explicitly call python.
Calling python tells the OS: "hey! open that program called python and tell it to run the file Welcome.py". This is exactly what the command /bin/python Welcome.py does on a unix system
When you install python on windows with a regular installer, .py files are associated with the python.exe you installed. When you type Welcome.py, Windows searches the local directory and then all paths in the PATH variable for a program called Welcome.py and runs it via python. Since this worked for you, it means that Welcome.py is somewhere on your path or in your local directory.
You can figure out your file associations with the assoc .py and ftype Python.File commands. The echo %PATH% and echo %PATHEXT% commands are also useful.
When you type python Welcome.py, Windows searches all paths in the PATH variable for a program that starts with 'python' and ends with an extension in PATHEXT. It finds 'python.exe' and runs it. Python in turn looks for a script called Welcome.py in the current directory. Since this didn't work for you, it means that Welcome.py is not in your local directory. It would have worked if you had given the right path to Welcome.py.
You can find out where Welcome.py is with the (not surprisingly) where Welcome.py command.
If you only have a single python installation, there is no need to call python myscript.py ....

Python correctly finding and reading windows application event logs

so my ultimate goal is to use python to read a specific application's windows event log when triggered by a file update.
Here is my problem, python I believe does not have access to the event logs stored in C:\Windows\System32\winevt\Logs. Whenever I try to read the files I get the following error:
WindowsError: [Error 2] The system cannot find the file specified
I tried every form of escaping, string split/join and using quotes on the file path and I always get the same error. I even cheaply used the os.system('dir "C:\Windows\System32..."') command in the python command prompt to list directories higher in the path for the log to verify access and I receive similar errors up to the C:\Windows\System32 directory, that one will list just fine.
Example:
C:\Windows\System32\winevt\Logs - File not found
C:\Windows\System32\winevt - File not found
C:\Windows\System32 - Lists files
I know these directories exist. So from here I figured I could use a bash script to copy the event log into a temp folder for python to read. I wrote a real simple bash script containing:
xcopy /Y "C:\Windows\System32\winevt\Logs\XXXXXXX" c:\Temp
(XXXXXXX) being the name of the log I want to copy for the python script.
The bash script runs fine on its own but when called by my python script it fails, refuses to copy the file because it can't find it. I have even tried using py2exe to create an exe to then run in administrator mode. This failed too. With similar errors of the file not found. I'm not sure I understand the permissions python uses to call the script and why the bash script cannot copy the file even when it can do it in a normal command prompt.
Any suggestions or flaws you can point out in my logic would be great. I am very stuck.
You are using a 32bits python on a 64bit install of windows.
In most cases, whenever a 32-bit application attempts to access %windir%\System32, the access is redirected to %windir%\SysWOW64
You can use os.listdir("c:\windows\sysnative\winevt\logs") as a workaround to read from the real system32 dir from a 32bit python interpretter runing on a 64bit windows...
Sources:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa384187(v=vs.85).aspx
http://support.microsoft.com/kb/942589

script file not found when using cmd.exe

I'm just getting started with Python and am trying to run a program from the command line, as it is done on this website under the heading "Python Program". So I made script hello.py, it's located in my computer at C:\Python27.
In the example, they run the script by typing python hello.py Guido. When I try to do this, it doesn't work. Firstly, I'm not entirely sure what is meant by the 'command line', but I'm using cmd.exe in Windows XP. I get this:
python: can't open file 'hello.py': [Errno 2] No such file or directory.
I have already specified PATH as C:\Python27.
Also, when I try to run the program from the Python shell by typing hello.py Guido I get
SyntaxError: invalid syntax.
When you start cmd.exe, the default directory is your Documents and Settings: since your file hello.py is not there, the python interpreter can't find it, thus giving you the [Errno 2] No such file or directory error. To solve that, just change your current working directory:
C:\Documents...>cd C:\Python27
C:\Python27> python hello.py Guido
Anyway, it is a good approach not to having your files inside the python directory (create a directory in your documents for python sources and use the same approach).
When you are running the python shell, you cannot explicitly call python files, so in your case it tries to run hello.py as a command (which doesn't exists) and it gives you a syntax error.
You need to locate your cmd current directory at C:\Python27:
cd C:\Python27
because the path python loads is relative. You can also use a full path:
python C:\Python2.7\hello.py
Try without "python", when you put python directory in path, it automatically connects ".py" extension with python, so there is no need in writing "python hello.py Guido"
Just go to directory where .py is located, and call "hello.py"
What's your current working directory and where is hello.py located? To execute that command, hello.py should be in the same directory from where you started the commend line (cmd.exe). Otherwise you need to the write the absolute path of hello.py (like python C:.....\hello.py Guido) instead of just the filename 'hello.py'.
I had also this problem but because of ether reason: I accidently added spaces to the names of some of the file's names, so the CMD didn't recognized the names.
for example: 'run .bat'

Categories