Skip to main content

Python script mysteriously refuses to run on Unix


Introduction

I was writing a new Python script (actually, a replacement for a bash script) at work today. I have a Windows laptop at work and in my projects and I often do development on Windows but the real objective is for the scripts and other work to run in a Unix system running under VirtualBox and Vagrant. To be exact, the system is:
Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-143-generic x86_64)

Anyway, I was writing away and it worked ok up to a point on Windows but it needed resources on the Unix system so you might say it failed miserably but I was ready to try it on the virtual machine. When I tried, I got:
(venv) vagrant@vagrant-host:/vagrant$ bruno/foobar.py --foo
: No such file or directory
(venv) vagrant@vagrant-host:/vagrant$

What the hell? That's a terrible error message. As an aside, I think some Python error messages can be awful and don't do a good job at fixing the problem. I think Python is a superb language but its error messages are not always the best.
Anyway, the error looked familiar but I couldn't remember what was wrong or how to fix it right off the bat. Some other facts:
  • The script uses #! /usr/bin/env python because the virtual machine has a newer version of Python in the $PATH which is supposed to override the system Python and I wanted to make use of it.
  • The script works fine if I do type python bruno/foobar.py --foo from the virtual machine command line but I don't want to have to do that.
  • I created the script on Windows using gvim
  • Remember, the script works fine from Windows!

Long story, short

After battling with this for several minutes, it finally dawned on me that I could be a poor victim of the Curse of the Carriage Return! When you create files on Windows, the file often has a carriage return character the precedes every newline character. Windows notepad is notorious for that - not only does it use carriage returns when you save a file but if you try to open a file that doesn't have carriage returns, it kind of acts like there are no newlines at all. Sometimes this can cause problems if you try to use the file in Unix or you might see funny symbols when you edit the file. There might be an option to not do that with gvim - I'll have to check that out.

I have a script called nocrs that can remove carriage returns on a file. Using that fixed up my script! Gosh, I just realized it's a bash script but that's ok with me.

Comments

  1. Recently I went back and rewrote my nocrs script as Python. I think I might have even made use of the logging and argparse modules. I've been loving using those lately.

    ReplyDelete

Post a Comment

Popular posts from this blog

Dynamic Python script loading

I have a bunch of toys and tools in a Git repository - I affectionately call this my toys repo . Most are just scripts that I use from a Unix (Cygwin or Git bash on Windoze) command line but there are some Python classes that I sometimes use in another script. Today at work, I was coming up with a new Python script that made use of a couple of my public classes. The script is good enough to share with my colleagues but I'm faced with the problem of my public classes: I imagine that most of my colleagues haven't even heard of my public classes and I don't expect them to download the entire repo just to get a couple of classes If I'm going to distribute the classes as separate files I introduce new problems: It could be confusing to have the files tag along. What is the user supposed to do with them? The answer is nothing - they should leave them alone and make sure they are in the same directory as the main script in case they decide to move th...

Git-based version information from Python script

I had this idea of generating version information for a Python script that uses ArgParse . The code is a little more than I was expecting but I think it works well. Here is the code: Usage Here is an example of its usage if the script is part of a git repository: $ ./version-example --version b92798, master, 2019-01-18 10:35:02, ['origin:https://github.com/pfuntner/gists.git'] $ It contains: The SHA1 of last git commit that changed the script The current branch of the repository The date of the commit - I think the timezone element is present in this but I didn't want to deal with timezones so I'm ignoring it A list of the remote repositories This is printed on two lines but that's something that ArgParse is doing, not me. Here is an example of its usage if the script is not part of a git repository - we don't have much information to work from but we can at least get the timestamp of the script: $ ~/tmp/version-example --version 2019-01-...

Extra Cygwin packages

Extra Cygwin packages I'm a big fan of Cygwin on Windoze to get the Unix/Linux look and feel. I can't stand the Windoze command prompt and their Power Shell is ridiculous. I don't want to get on another rant but I thought Power Shell was supposed to be their attempt at making Windoze more like Unix but it isn't. Yes, I have looked into WSL and it's ok but I'll stick with Cygwin. One of the things I don't like about Cygwin is that their default list of packages just isn't enough for me. After I install it on a new system, I often find myself adding additional packages one-at-a-time when I discover I need them. I finally decided to be a little proactive and keep track of packages I had to install. It looks like my beautiful formatting of this page has been trashed somehow and I apologize for that. I'll work on it. My extra packages Package Comments git Gotta have this! pip2 Yes, I'm still a proud Python2 guy! An...