set path for python command in shell(mac) - python

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"

Related

File in the same folder but still get FileNotFoud

I am trying to open a .dat file by Pickle. Although the file is at the same folder as my .py file, when I run I get a FileNotFoundError.
(FYI: I am using VScode to run the file, however, it runs perfectly fine when I use Terminal)
here is my code
import pickle
websites_list = pickle.load(open("websites.dat","rb"))
print(websites_list)
This is the .py path:
/Users/lequangdang/Documents/WSC/WSC_2.0/changewebsiteGUI.py
here is the .dat path: /Users/lequangdang/Documents/WSC/WSC_2.0/websites.dat
Here is the error:
FileNotFoundError: [Errno 2] No such file or directory: 'websites.dat'
The relative path is base on work directory when using vscode, but is base on py file itself when using terminal.
For example the directory structure seems like:
WSC---WSC2.0---changewebsiteGUI.py
when you open WSC in vscode, pickle.load(open("websites.dat","rb")) will call the path WSC/websites.dat and that's why you have FileNotFoundError
one way to solve this is to set the vscode launch.json, add this line:
"cwd": "${workspaceRoot}/WSC2.0"
the other way is to use some function to get the abs path of pyfile itself:
os.path.dirname(os.path.abspath(__file__))

I am unable to run a python file

I get this errors quite often when trying to run a python file using CMD :
Can't open : [Errno 2] No such file or directory.
I have tried both: cd <path> where path is folder where file is saved; and python <path\filename> . Both give the exact same error.
How to get rid of it?

Docker Run [tag-name to container] returns python: can't open file in Windows

python: can't open file 'Test.py': [Errno 2] No such file or directory even though I have specified the working directory of the container.
CMD path
Try using full path when you try to run something.
/home/user/Test.py

Python can't run absolute script?

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

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.

Categories

Resources