Python can't run absolute script? - python

I encountered such a weird situation:
naivechou#naivechou/~>python test.py
test
naivechou#naivechou/~>pwd
/home/naivechou
naivechou#naivechou/~>python /home/naivechou/test.py
C:\toolchain\python\python.exe: can't open file '/home/naivechou/test.py': [Errno 2] No such file or directory
My working directory is /home/naivechou/, test.py is in there.
If I run test.py with absolute path, I'll get an error message of No such file or directory.But everything will be fine if I enter that directory and then run it. What's wrong with python?

Try moving into the folder where the python script is located and do a "ls" command there in Linux. if windows then do 'dir'. if you see the required file there then execute the following command
C:\location_where_the_script_is> python yourfile.py

For commands entered on the command line, Windows doesn't recognize forward-slashes as directory separators.
Your second example is looking in the current directory for the literal filename /home/naivechou/test.py, and of course such a filename does not exist.
Use backslashes instead, as is the Windows way:
python \home\naivechou\test.py

Related

cd directory_name python python_file.py gives error in command line " the system cannot find the path specified"

I am running python file from command line. To be more specific my .py file is in location F:\new_github_code\cocktail_party\data\audio\audio_downloader.py>. I want to run audio_downloader.py file from the cocktail_party directory not from the audio directory.
I tried by using the following script from cmd. Any help will be appreciate
enter image description here
Try this. It will work:
python data\audio\audio_downloader.py

set path for python command in shell(mac)

I want to run python command as follows, without the whole path of pyahp or other scripts. It reported the error. I have to add the whole path, ie .../site_packages/ that is tedious. How can I run it directly?
[~] python pyahp
/Library/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python: can't open file 'pyahp':
[Errno 2] No such file or directory
I trid to add pythonpath, however it failed.
export
PYTHONPATH="$PYTHONPATH:/Library/Frameworks/Python.framework/Versions/3.6/lib"

Opening a python file in terminal. (Ubuntu)

I just installed ubuntu today, and I can't figure out how to run my .py file.
I opened terminal and typed in python test.py and I receive an error saying "python: can't open file 'test.py': [Errno 2] No such file or directory"
If you could please help me out, that would be great.
Thanks
You are using the correct command to open it, but there is no test.py file in your current working directory.
Try using
ls
To verify what files are in fact in your working directory and use
cd path/goes/here
To change directories to where the file is located.
Then try python test.py
python test.py would try to execute the test.py from the current directory.
else try giving the complete path of the test.py
like "python /usr/local/bin/test.py"

How to set the correct path for a file in VIM?

Whenever I hit :pwd in vim the command always returns the path C:\Windows\system32, even if I'm in a Python file from the desktop. So whenever I run :!python % the command returns
python: can't open file '\Users\myname': [Errno 2] No such file or directory.
But if I set the path with the command :cd %:p:h and then run the same python command the Python file executes correctly. So basically I'm wondering how do I get vim to correctly set the path for every file that I open.
(i.e. if I'm in a file located at the desktop :pwd returns ~\Desktop\ or if I'm in a file in the home directory :pwd returns C:\Users\MyName\).
You can set autochdir:
:set autochdir
With this setting the current working directory will follow the file that you're editing.
See :help 'autochdir', and particularly this note:
Note: When this option is on some plugins may not work.

Relative Python Path to Script

Python project looks like this:
setup.py
README
Application
scripts
hello.py
shell_scripts
date.sh
From hello.py I'm executing the command subprocess.call(['../shell_scripts/date.sh']) and receiving the error OSError: [Errno 8] Exec format error.
Note: date.sh is a perfectly valid shell script and is executable. I've also tried os.path.realpath to no avail.
I assume this is due to an invalid path?
Exec format error will come when the shell isn't set at the script. try adding #!/bin/sh at the beginning of the script and execute the python script.

Categories

Resources