How can I make a .command file?

Hello! I was wondering how can I make a .command file?

It needs to cd to a folder, active a VENV, cd to the main folder with the python file, and then run the python file. Can anyone help me out?

iMac Line (2012 and Later)

Posted on Mar 17, 2021 9:34 AM

Reply
Question marked as Top-ranking reply

Posted on Mar 17, 2021 9:49 AM

It can just be a regular Bash or Zsh script with a .command extension that has executable permissions. Then you double-click that .command file from the Finder, it launches a Terminal window and then runs the contents of the shell script in that Terminal window.


For instance:


#!/bin/zsh

# activate Python 3.9.2 in my VENV
source ~/py39venv/bin/activate
# change directory to where my Qt 5.15 Python script is located and run it
cd ~/Desktop && ./tqt.py
# shutdown the VENV
deactivate
exit 0


9 replies
Question marked as Top-ranking reply

Mar 17, 2021 9:49 AM in response to Cameron-J-R

It can just be a regular Bash or Zsh script with a .command extension that has executable permissions. Then you double-click that .command file from the Finder, it launches a Terminal window and then runs the contents of the shell script in that Terminal window.


For instance:


#!/bin/zsh

# activate Python 3.9.2 in my VENV
source ~/py39venv/bin/activate
# change directory to where my Qt 5.15 Python script is located and run it
cd ~/Desktop && ./tqt.py
# shutdown the VENV
deactivate
exit 0


Mar 17, 2021 10:45 AM in response to Cameron-J-R

If your venv is located here:


cd /Users/cjr/Desktop/TestServ/crafty/crafty


then your invocation of the activate must be done like so:


source ./venv/bin/activate


and if your crafty.py begins with:


#!/usr/bin/env python3


there would be no need to start it with:


python crafty.py


cd /Users/cjr/Desktop/TestServ/crafty/crafty
source ./venv/bin/activate
cd ../crafty-web && ./crafty.py

Mar 17, 2021 10:31 AM in response to Cameron-J-R

/Users/cjr/venv/bin/activate\ufeff


My Python 3.9.2 VENV uses just activate, not activate\ufeff which is both an illegal UNIX path construct, and the VENV/bin has no such executable with a Unicode name ending. Make certain that the directory hierarchy that you are using in the script actually exists beforehand.


Why bother to do the following:


cd ~/TestServ/crafty/crafty


when you are then doing undoing the preceding in:


source ~/venv/bin/activate
cd ~/TestServ/crafty/crafty-web && ./crafty.py


Mar 17, 2021 10:04 AM in response to VikingOSX

#!/bin/zsh

cd ~/TestServ/crafty/crafty

source ~/venv/bin/activate

cd ~/TestServ/crafty/crafty/crafty-web && ./crafty.py


I used that but it's giving me this error:

/Users/cjr/Desktop/TestServ/crafty.command ; exit;           

cjr@Cams-iMac ~ % /Users/cjr/Desktop/TestServ/crafty.command ; exit;

/Users/cjr/Desktop/TestServ/crafty.command:cd:2: no such file or directory: /Users/cjr/TestServ/crafty/crafty

/Users/cjr/venv/bin/activate\ufeff:source:3: no such file or directory: /Users/cjr/venv/bin/activate\ufeff

/Users/cjr/Desktop/TestServ/crafty.command:cd:4: no such file or directory: /Users/cjr/TestServ/crafty/crafty/crafty-web

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How can I make a .command file?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.