Python script gives `: No such file or directory`

丶灬走出姿态 提交于 2019-11-27 11:40:15

From the comments above it looks like you have dos line endings, and so the hashbang line is not properly processed.

Line ending style are not shown with :set list in Vim because that option is only used when reading/writing the file. In memory line endings are always that, line-endings. The line ending style used for a file is kept in a Vim per-file option, weirdly called fileformat.

To see/change the line ending style from Vim, you can use the following commands:

:set fileformat
:set ff

It will show dos or unix. You want unix, of course ;-).

To change it quickly you can save the file with:

:w ++ff=unix

Or if you prefer:

:set ff=unix

And then save the file normally.

So see all the gory details just do :help fileformat, :help file-formats and :help fileformats

You can also use the dos2unix command to convert the file format

dos2unix

This helped me to run the python scripts

This normally happens when we open files in windows do changes and save it. if you open the file locate the ^M characters at the end of every line

Thanks

Personally, I find it kinda wrong using direct path to python interpreter. As you dont use windows platform, you should have program env, usually in /usr/bin (/usr/bin/env). Try using following shebang:

#!/usr/bin/env python

Different distros store python binary in /bin or /usr/bin (or some weird locations), and this one makes your script config-independent (as far as possible, here we have possibility that env is stored elsewhere; still - it is less possible that env is not in /usr/bin than that python is mislocated).

I had similiar problem (if not exactly the same) and that worked for me.

Also, I have both python interpreters (2.7.x and 3.x) installed, so I need to use "python3" argument for env. AFAIR usually distros link different names to different binaries, so "env python" will run python2.7 on my system, "env python3" (also python33, or smth like that) will run p3k, and "env python2" (also python27, etc) will run python 2.7.x. Declaring which version of interpreter should be used seems like a good idea too.

I came across this problem editing my code on Windows, checking it in with git, and checking out and running it on Linux.

My solution was: tell git to Do The Right Thing. I issued this command on the Windows box:

git config --global core.autocrlf true

Modified the files and checked them in; voila, no such problem any more.

As discussed on the Git documentation.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!