Terminal Find Specific File - python

NOTE: This is for MacOSX
I am working on a launcher for an app and the only way I can make terminal find the file is putting the direct root.
If I can get around this so it would work in any location what would the code be?
Here is the code in the executable:
#!/bin/bash
python /Users/imac/Desktop/Notepad+/Contents/main.py

The path always will change from computer to computer. You can run executable in the same directory with
python main.py
for python 3
python3 main.py
Your shell script will be like.
#!/bin/bash
python main.py
I hope it will help

Related

How can I export my spotipy code efficiently?

I wrote a python program using the spotipy library. I used pyinstaller to create executables and it works fine for me. However, when I try to email it to someone (via gmail, sent through imessage) it doesn't run correctly. We are all on Mac OS X. When they try to open it with terminal, it opens terminal but the program doesn't run. I created the executable with the following command:
pyinstall -F example.py
and I sent the executable in the dist folder. I've never really tried to distribute any code before so any help would be appreciated. Thanks.
For XOS and if you gonna use pyinstaller:
pyinstaller
Put this line at the top of your python code and it will inform your operating systems to look up the binary program which needs for the execution of the python script for example it is the python interpreter.
Therefore it depends on your operating system where it keeps the python interpreter. If you have Ubuntu as operating system it keeps the python interpreter in /usr/bin/python so you have to write this line at the starting of your python script;
#!/usr/bin/python
Write above line at the top of your python code
Saving your code
Start your command terminal
Make sure the script lies in your present working directory
Type chmod +x script_name.py
Now you can start the script by clicking the script. An alert box will appear; press "Run" or "Run in Terminal" in the alert box;
Or, at the terminal prompt, type ./script_name.py
For Windows as your OS.
Make sure the python script contains:
if __name__ == '__main__':
main()
Make sure you do not have a folder with the same name as the script you are trying to turn into an executable.
Make sure you do not have any .ipynb in the same folder where you will create your executable, it will not run properly.
You can follow these instructions:
Making Executable Python

How to write makefile for Python(turn into a executable file)?

I need to write a makefile to turn my python codes into executable file. For example, I have main.py , utility.py. I need an executable file name "run" such that ./run can be executed. I tried "python main.py run" in the Makefile but it says that missing separator ?
I'm sorry if I'm missing something, but why wouldn't a .py script be executable?
As long as the Python interpreter is installed in the system, you can run .py files from shell. I don't see the need for you "run" script.
In any case, you could create a script with the command you want to execute, in this case:
python main.py
or
python main.py && python utility.py
to run more than one python script at once.
(Always paying attention to the real name of the python interpreter present in your system. It could be python37, python27...)
Maybe there is something about your particular problem that I'm not getting right...
I'm happy to keep discussing

specify the python interpreter path in python script

I have a python script which I am distributing in several nodes. I have python 2.6 installed in /usr/bin by default and have python 2.7 in my /opt directory in all the nodes. Now when I run the script from my current node I can set the path to python 2.7 interpreter from terminal but I am unable to manage it in the rest of the nodes where this script is getting distributed. I have added the shebang at the start of script like -
#!/opt/python2.7/bin/python
But its still not working. How can I change the python interpreter/python path at the beginning of the script itself.
What you explain should work but check:
that the script is executable (chmod +x my_script.py if required).
that you are calling the script directly and not using another Python interpreter (check that you execute ./my_script.py or /path/my_script.py and not python my_script.py).
To help to diagnose the problem you could add the following lines to the top of your script:
#!/opt/python2.7/bin/python
import sys
print(sys.executable)
if the output is not /opt/python2.7/bin/python you might be calling the script with another interpreter.
If for some reason you can only call scripts executed by the 2.6 version of Python remotely but you can also distribute additional files, you could try to send your main script somewhere and execute the following auxiliary script:
from subprocess import call
call("/opt/python2.7/bin/python /path/my_scipt.py", shell=True)

Trying use a .pth file to add a path in documents folder on Mac

Hi everyone I'm trying to get Python configured on an OS X laptop and I'm having some trouble. I'm both new to Python and am very unfamiliar with the UNIX terminal. What I'd like to be able to do is to have a directory in my documents folder that would contain python modules and be able to run them from the command line. Currently I have a Python Directory and a chaos.py module inside of it. The full path is /Users/Ben/Documents/Python/chaos.py.
So I followed the steps here and here. I can see that the site-packages for Python 3.4 is in a few spots but I chose this one: '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages' to place the .pth file.
I created a file named Ben.pth in this location with the contents: /Users/Ben/Documents/Python/
Now from my (very limited) understanding that should be all I would need to do for Python to start looking right? So I try to run python3 chaos.py in terminal and I get an error:
/Library/Frameworks/Python.framework/Versions/3.4/Resources/Python.app/Contents/MacOS/Python: can't open file 'chaos.py': [Errno 2] No such file or directory
I'll also try opening IDLE clicking File->Open Module... and try to open it from there and I'll recieve a "module not found" box.
I'm completely stumped, I'm not sure if its a syntax error that I made somewhere (again I don't really know what I'm doing with the UNIX commands) or if I'm just way out in right field. If anyone could help me out, I would greatly appreciate it! Thanks!
Forget the .pth stuff for now, that's not something you'd normally do. In a unix-ish environment, the typical way you'd run a script would be to change directory:
cd /Users/Ben/Documents/Python/
and then run the script:
python chaos.py
Another way to do it would be to run the script with an absolute path; you can do this no matter your current working directory:
python /Users/Ben/Documents/Python/chaos.py
Finally, if you've written a utility script you want to be run from anywhere without typing that absolute path all the time, you can do something a little fancier...
Add a 'shebang' line as the first line of your script. It'll go like this:
#!/usr/bin/env python
Get into the directory where your script lives:
cd /Users/Ben/Documents/Python/
Make the script executable:
chmod +x chaos.py
Put a link to the script in a directory on your path... /usr/local/bin/ could be a good choice:
ln -s /Users/Ben/Documents/Python/chaos.py /usr/local/bin/chaos.py
Now you can type chaos.py anywhere on your system and it'll run.

Can't run Python .py files from terminal on Mac

I just downloaded Python 3.2 to Mac OS 10.6 environment. I'm new to programming and am trying to run my first stand-alone .py file, but I keep getting an error message saying "no such directory or file." The name of the file is "script1.py" and I saved it to /Users/billp/Documents. When I open the Terminal to run the file I type:
python script1.py
I've also tried adding this line to the beginning of the script:
#!/usr/local/bin/python
As well as this one:
#!/usr/bin/env python
Yet, I keep getting the same error message. Any help would be greatly appreciated.
Make sure you are in the right working directory after opening terminal. Type
cd /Users/billp/Documents/
(use tab to autocomplete)
then
python ./script1.py
This way you are launching python executable and passing it path to your file as the first argument. The shebang #! line you mentioned allows you to launch your script directly, like this: ./script1.py, but you need to mark the file as executable chmod +x script1.py and provide path to interpreter (python) after the shebang. #!/usr/bin/env python references your default python installation.
The ./ stands for current directory. By default when you type script1.py your shell (which is the thing that you type commands into, through the terminal) would look for executable file in special folders listed in PATH env variable. script1.py is not usually there, so you would see -bash: script1.py: command not found. python, on the other hand is on the PATH so it should work.
Maybe you forgot to make the file executable? Try this at the command prompt:
$ chmod +x script1.py
I prefer to start my Python scripts in a Mac with these lines (assuming of course that you're saving the file in UTF-8 encoding:
#!/usr/bin/env python
#coding=utf-8
Also, make sure that the pythoncommand is available in the path. If everything is set up correctly, it won't be necessary to type python first, and you can run the script directly by typing ./script1.py in the directory where it is located.
One final thing, for running a piece of code when executing the script from the command line (as opposed to simply loading the definitions in the file), write this at the end:
if __name__ == '__main__':
# the code you want to call
Are your python binaries here?
/Library/Frameworks/Python.framework/Versions/3.2/bin/python
It's worth pointing out, as long as the file is in your current directory it's automatically available. Otherwise, anything files will have to be referenced absolutely using the full path information.
So the following examples are calling the same file:
Explicit (absolute path)
python /Users/billp/Documents/script1.py
python /Users/billp/Documents/script2.py
python /Users/billp/Documents/script3.py
Implicit (relative path)
cd /Users/billp/Documents
python script1.py
python script2.py
python script3.py
As long as you're working with files in the same directory (commonly known as your working directory), you may always safely use relative paths. If the files are anywhere else, you must always specify an absolute path.

Categories

Resources