I need to run a python file using an older python 2.6 version installed with an external software.
To do this I have resorted to using os.system such as
os.system('""path/old_python.exe" "file.py""')
(note that the odd no. of ('") is due to the path containing a space c:/program files (x86)/.. as I am running on windows.)
This code string works well if run from a root directory. However, I would like to place this os.system call in a module within a sub-package to my root and thereafter run it from a root module. So the hierarchy would look like this:
/root
call_os_module.py
/subpack1
os_module.py
file.py
If I run this I receive the error:
path/old_python.exe: can't open file 'file.py': [Errno 2] No such file or directory
I have added the full path to subpack1 to sys.path. However, I still receive the same error and os can't find the file. How do I solve this?
Have a look at these questions:
How in Python to run external Python script with the current Python interpreter?
Relative paths in Python
in general: https://stackoverflow.com/search?q=python+relative+path
Related
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.
I feel a little foolish that I don't know this, but I tried to do it today and was surprised when it didn't work....
I have a directory C:\test with a demo script, lets call it demo.py
If i am in C:\test then I can just do python demo.py. Easy
I could also use a relative path, so from C:\, it's python test\demo.py
What if C:\test is on the path?
I was expecting to be able to now do python demo.py from anywhere however...
python: can't open file 'demo.py': [Errno 2] No such file or directory
I feel foolish because I thought this was straightforward, but I have searched around and have not found a solution. Am I fundamentally misunderstanding something here about how the Python interpreter finds scripts to run? I don't think this is anything to do with PYTHONPATH, as I understood that to relate to loading of modules inside scripts.
This is on Windows 7, by the way.
The PATH is only used to search for commands. A first way is that a Python script can be used directly as a command and in that case the PATH will be used: just use demo.py instead of python demo.py.
It will rely on OS specific ways. On Windows, file type (given by the extension - here .py) can be given default application to process them, while on Unix-like, the first line of a script can declare the program that will process it.
Alternatively, python allows to launch a module that will be searched in the PYTHONPATH (not PATH) by using python -m module or for Windows py -m module.
So I have been trying to open very basic files like this: inputFile = open("keywords.txt", "r")
But I get this error:
FileNotFoundError: [Errno 2] No such file or directory: 'keywords.txt'
This must be because Python's default working directory is not the one where my .py file is (correct me if i'm wrong).
How can I find the exact path of the directory I'm working in and then change Python's default working directory to it?
Note: I'm on OSX Mojave using VSCode and Anaconda (Python 3.6)
We have to distinguish the two notions: the "working directory" and the "python environment".
When you go into VS code from Anaconda, then you are in an Anaconda virtual python environment and it's default directory.
You can test it with upon-left first icon - "Explorer".
Here you will find a directory structure, where your .py file actually is.
But sometimes the file what the .py searching for could be in some quite other directory. If you want to run a python program with some additional files stored in a common directory, then I prefer to use some variable to set the working directory:
work_dir = "path/to/working_directory"
The use
import os
path = os.join(work_dir, somefile.txt)
If the files the python program uses are in a complex directory structure, then changing the working directory won't be a solution.
Though when the programmer uses relative paths, it can be.
In VS Code has a terminal where you can check the current Anaconda environment and directory in a bash shell manner.
When you run the program you will see what environment and options your .py file is actually running in and with.
By default Python's "current working directory" is the folder which it is placed in. Though this might be different when using Anaconda
This will print your current directory
import os
print(os.getcwd())
This will change your directory
os.chdir('Your new file path here')
Then do another print(os.getcwd()) to make sure it worked
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'
I have installed http://ftp.logilab.org/pub/pylint/pylint-0.18.1.tar.gz on Windows and now I am trying to configure my Emacs's flymake mode using epylint script.
Here is the output of I got when I tried epylint on windows command prompt.
C:\>epylint test.py
'test.py':1: [F] No module named 'test.py'
Any suggestions on how to fix this problem?
Reading the documentation of the epylint.lint function:
When run from emacs we will be in the directory of a file, and passed its filename.
If this file is part of a package and is trying to import other modules from within
its own package or another package rooted in a directory below it, pylint will classify
it as a failed import.
To get around this, we traverse down the directory tree to find the root of the package this
module is in. We then invoke pylint from this directory.
Finally, we must correct the filenames in the output generated by pylint so Emacs doesn't
become confused (it will expect just the original filename, while pylint may extend it with
extra directories if we've traversed down the tree)
It sounds like it has to do some extra magic to work within Emacs. It doesn't look like you can run it the same way from the command line.
Is it not working for you from within Emacs? It might be a bug in pylint then. Does pylint have a mailing list you can report issues to?