OS: Mac 10.14.6
Python Version: 3.8.5
New to Python and Bash so apologies if this is a dumb question but I can't find an answer anywhere. The closest I found was this answer on this thread however, I've already executed chmod +x on that file to change the permissions to allow it to be executable and I followed the instructions again and I still couldn't get it to work.
Basically I want to run Python scripts from a specified folder on my desktop (file path ~/Desktop/Python\ Scripts) through Terminal without having to change directories (out of pure laziness).
I added the folder to PATH and can see that it is listed when I run echo $PATH in Terminal. I thought that would do the trick but when I try to run the program with the command python boxprintV2.py as I usually would when I change directories I get python: can't open file 'boxprintV2.py': [Errno 2] No such file or directory
This command works fine if I change the current directory as I have been doing and I can run my program no problem but I would like to run from a new terminal window without having to change directory every time. Permissions on the file have been changed using chmod +x
Shebang from my program is #!/usr/bin/env python3.
If you run the command python <filename>, the Python interpreter will only check the current directory. Therefore, this only works if your working directory is "~/Desktop/Python Scripts", as you have already found out.
Because your script is marked as executable and it includes a shebang at the beginning of the file, you can just execute it directly from the command line by only entering boxprintV2.py. Bash will then search all directories in $PATH for this file and execute it.
Ok, I've found a workaround by creating a shell script following this answer on a different thread.
What I did was open a blank textedit file, go to format and convert it to plain text (or ⇧ + ⌘ + T which toggles rich text/plain text).
From there I typed these commands into the document as follows:
#! /bin/bash
cd ~/Desktop/Python\ Scripts
python boxprintV2.py
When I saved I didn't specify a file extension and unticked the box that said "If no extension is provided, use .txt". I'm not sure if this was necessary but I'm just detailing my exact workflow for anyone else who may have the same (laziness) problem as I do.
I then went back into a blank terminal window and entered:
chmod +x ~/Desktop/Python\ Scripts/boxprintV2to allow the shell script to be executed by all users.
From here I can just open the Python Scripts folder on my desktop, double click on the plain text file which is now a .exe and a new terminal window is opened with my Python script running!
It's literally going to save me tens of seconds of my life. I'm sure I'll waste them anyway.
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
I am having a tough time overcoming this error, I have searched everywhere for that error message and nothing seems relevant to my situation:
"failed to execute script new-app"
new-app is my python GUI program. When I run pyinstaller using this command:
pyinstaller.exe --onedir --hidden-import FileDialog --windowed --noupx new-app.py
It does work smoothly. In addition, when I execute the command line to run the gui program, it works perfectly and the GUI is generated using this command:
.\dist\new-app\new-app.exe
But when I go to that file hopefully to be able to click the app to get the GUI, it gives me the error said above. Why is that?
I am using python2.7 and the OS is Windows 7 Enterprise.
Any inputs will be appreciated and thanks a lot in advance.
Well I guess I have found the solution for my own question, here is how I did it:
Eventhough I was being able to successfully run the program using normal python command as well as successfully run pyinstaller and be able to execute the app "new_app.exe" using the command line mentioned in the question which in both cases display the GUI with no problem at all. However, only when I click the application it won't allow to display the GUI and no error is generated.
So, What I did is I added an extra parameter --debug in the pyinstaller command and removing the --windowed parameter so that I can see what is actually happening when the app is clicked and I found out there was an error which made a lot of sense when I trace it, it basically complained that "some_image.jpg" no such file or directory.
The reason why it complains and didn't complain when I ran the script from the first place or even using the command line "./" is because the file image existed in the same path as the script located but when pyinstaller created "dist" directory which has the app product it makes a perfect sense that the image file is not there and so I basically moved it to that dist directory where the clickable app is there!
So The Simple answer is to place all the media files or folders which were used by code in the directory where exe file is there.
Second method is to add "--add-data <path to file/folder>"(this can be used multiple times to add different files) option in pyinstaller command this will automatically put the given file or folder into the exe folder.
In my case i have a main.py that have dependencies with other files. After I build that app with py installer using this command:
pyinstaller --onefile --windowed main.py
I got the main.exe inside dist folder. I double clicked on this file, and I raised the error mentioned above.
To fix this, I just copy the main.exe from dist directory to previous directory, which is the root directory of my main.py and the dependency files, and I got no error after run the main.exe.
Add this function at the beginning of your script :
import sys, os
def resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
Refer to your data files by calling the function resource_path(), like this:
resource_path('myimage.gif')
Then use this command:
pyinstaller --onefile --windowed --add-data todo.ico;. script.py
For more information visit this documentation page.
In case anyone doesn't get results from the other answers, I fixed a similar problem by:
adding --hidden-import flags as needed for any missing modules
cleaning up the associated folders and spec files:
rmdir /s /q dist
rmdir /s /q build
del /s /q my_service.spec
Running the commands for installation as Administrator
I was getting this error for a different reason than those listed here, and could not find the solution easily, so I figured I would post here.
Hopefully this is helpful to someone.
My issue was with referencing files in the program. It was not able to find the file listed, because when I was coding it I had the file I wanted to reference in the top level directory and just called
"my_file.png"
when I was calling the files.
pyinstaller did not like this, because even when I was running it from the same folder, it was expecting a full path:
"C:\Files\my_file.png"
Once I changed all of my paths, to the full version of their path, it fixed this issue.
I got the same error and figured out that i wrote my script using Anaconda but pyinstaller tries to pack script on pure python. So, modules not exist in pythons library folder cause this problem.
That error is due to missing of modules in pyinstaller. You can find the missing modules by running script in executable command line, i.e., by removing '-w' from the command. Once you created the command line executable file then in command line it will show the missing modules. By finding those missing modules you can add this to your command :
" --hidden-import = missingmodule "
I solved my problem through this.
I had a similar problem, this was due to the fact that I am using anaconda and not installing the dependencies in pip but in anaconda. What helped me was to install the dependencies in pip.
I found a similar issue but none of the answers up above helped. I found a solution to my problem activating the base environment. Trying once more what I was doing without base I got my GUI.exe executed.
As stated by #Shyrtle, given that once solved my initial problem I wanted to add a background image, I had to pass the entire path of the image even if the file.py and the image itself were in the same directory.
In my case (level noob) I forgot to install library "matplotlib". Program worked in Pycharm, but not when I tried open from terminal. After installed library in Main directory all was ok.
I'm creating a program that I would like to use as a normal program as well as continue to code it on the side. To do this I first tried creating a shortcut of the .py file in my PyCharms project folder and sent it to desktop. When I double-clicked the shortcut the command prompt would open for a second and then shut. It's a PyQt4 program so I'm not sure if this has any bearing. The program has been coded in Python 3.4. I've noticed that when I open the command prompt and type 'python' it shows Python 3.5 for some reason so I'm not sure if this has any bearing on the situation.
If you've ever programmed in C# I'd like to be able to build a solution and then rebuild the solution when I've updated the code so that I can access the program as a normal program as well as continue to improve the code of it.
Thanks for any help.
Managed it thanks to the link above. Uninstalled Python 3.5 and set my PATH variable to C:\Python34. Downloaded pyinstaller and installed it using PIP. Then navigated to Python34/Scripts and dragged myFile.py (the one to be made an .exe) into it. Ran pyinstaller.exe --windowed myFile.py to create the exe which then went to my dist folder. Created a shortcut and it worked perfectly.
Go to your environmental variables (Right click on Computer > Properties > Advanced system settings > Environment Variables...). Find Path in System variables, select it, and click edit. Remove the Python 3.5 path and replace it with your python 3.4 or virtual environment folder that has python.exe in it.
Make a shortcut on your desktop that points to the .py file that you are editing.
If you have all of the dependencies right then double clicking the .py file's shortcut should run your program.
Other wise you can pip install cx_freeze and use cx_freeze like setuptools. Create a setup.py file and build the executable.
If you want to install this executable I suggest using Inno Setup. It is pretty straight forward on how to use and has an easy wizard that helps you build a basic installer.
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.