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

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

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

Python 3.6.0 can't be installed on Ubuntu 16.04?

I wrote a script on my Windows machine using python 3.6.0. I wanted to run it on Linux machines as well so I transferred the .py script onto my Ubuntu VM. The problem is, I tried to install python 3.6.0 in every way possible but it doesn't really work. Python3 works when I want to execute a .py file, but since I am trying to use pyisntaller to convert it into an executable for linux, I need the default version as 3.6.0. python --version gives me Python 2.7.2. How can I fix this so when I do python --version it shows 3.6.0?
There is no point in changing your default python version, Ubuntu relies heavily on older versions of python. And what do you mean by convert it into an executable for Linux? Almost every file on your file system is an executable. If you want to make your program run as a command in the terminal you can do this though:
text_editor_of_your_choice .bashrc
This should open the hidden .bashrc file, and scroll to the bottom of it. Below is what the file looks like...
.bashrc
After that you can create a function for the python command you want to run.
eg. function ghst {python3.6 /home/user/example_script/example.py $#}
Start a terminal session and then try running it. Now that's if you want to run it inside the terminal. If it you want to create this "executable file" you speak of you can create a simple shell script.
First go in the terminal and run touch file_name_of_your_choice.sh
This will create a blank file named file_name_of_your_choice with the file extension "sh". After that open the text file in whatever directory you created it in, if you are unsure do pwd.
In your first line make sure that the user is inside the directory of the .py file by doing
cd /home/user/folder/ On the next line you should then execute the .py file. You can do this by typing this: python3 file.py. This will execute the program inside a terminal window. After you have done all of this make sure that the python file and the script file are executable. Do this by doing chmod +x file.py and chmod file.sh.
This is what the end result looks like
example script
You mentioned inside your question that you want your program to be able to run on Linux machines as well. Whether this is for personal or a public project of some sort it comes in handy by packaging all the required files in a .zip format this makes it easy to unpack everything when moving to separate machines. If there are any problems let me know.

Can a Script be an Environment Variable

I have a Python script that I would like to be able to execute from no matter where. Either in Linux or in Windows, but in this case preferably in Windows. Putting the path to the script into PATH under Windows did not work, so from some directory calling python my_script.py results in the message that there is no such file in this directory. So, is this somehow possible?
You can try creating an alias as such:
Linux
Create a .bash_aliases file on your home folder
Add an alias such as alias pscript='python /home/pythonscript.py'
Log out and back in or do a source .bash_aliases
Windows
Run doskey pscript=python C:\script.py. Read more here
The PATH is used to search for executables, and this doesn't include in windows script files.
A workaround is to convert the script to a batch file, see here
how to simply have the script act also as a batch file
For the operating system to run your script, it needs to find it (the PATH variable), recognize that it is executable and know which program should execute it. You seem to have the PATH part handled, so now for the other two.
On unixy systems you need to make the script executable. To set the user-executable bit, do chmod u+x myscript.py. Then you need to tell the system which program should run it. Typically you use the "shebang" as the very first line in the file:
#!/usr/bin/env python3
The system will search the path for a program called "python3" (use "python" for python 2 scripts) and use that executable to run the script.
On Windows, you need to associate the file extension (.py) with the python exeuctable. That's usually done for you when python is installed. If not, you can dig into ftype, assoc and pathext here.
Windows doesn't care about the shebang (unless you are running cygwin, then see unixy systems above) so the same script can live in both worlds.
Once the script is executable, you call it directly instead of executing python and giving the file as the script name. Its just
myscript.py

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.

Python path help

how can i run my program using test files on my desktop without typing in the specific pathname. I just want to be able to type the file name and continue on with my program. Since i want to be able to send it to a friend and not needing for him to change the path rather just read the exact same file that he has on his desktop.
f = open(os.path.join(os.environ['USERPROFILE'], 'DESKTOP', my_filename))
If you are on Unix/Mac you can add a shebang at the top of your script and set it as executable. Usually you'd do:
#!/usr/bin/env python
And then to make it executable, from a terminal use chmod:
chmod +x script.py
Now you can run the script (if you are in the same directory) like:
./script.py
If you are on Windows you'll want to add the Python executable to your %PATH% enviromental variable, then you can run your script with python script.py. They talk about this in more depth in the Python documentation: http://docs.python.org/using/windows.html
If you place your Python script in the same directory as the files your script is going to open, then you don't need to specify any paths. Be sure to allow the Python installer to "Register Extensions", so Python is called when you double-click on a Python script.
You can tell your friend to make *.py files to be executed by the interpreter. Change it from Explorer:Tools:Folder Options:File Types.

Categories

Resources