So I'm trying to share my PyQt project. When I download the zip file and extract it, it looks like
If I run app.py from CMD, it will run the app, but without the icon file which is inside of that folder. Inside of the code I do need that file and point to it, so I'm not sure why it doesn't find it automatically. It seems that without it the app doesn't work properly. I was wondering if there's a work around for this issue.
Here is how the app looks when I "open folder" in my IDE:
Here is how it looks when I simply open the .py file, in that same folder:
Anything related to the icons (basically all notifications) are not working when I run it like that.
I'm not sure what why it behaves like this, but I'd like to be able to share the code for anyone to use without them opening the whole folder.
Eventually, I ended up changing how I'm using the paths.
I added this
dirname = os.path.dirname(__file__)
iconFile = os.path.join(dirname, 'icon/icon.png')
So now I'm using iconFile as my path. Seems to fix the issue
Related
I've been trying to access a folder outside my current folder, however when I Use "../images/image.png" it gives a FileNotFound error. Now running this code in IDLE gives no error and works perfectly. How can i use ../ in Vs code?
Using pygame.display.set_icon(pygame.image.load(f"./images/ui/logo.png")) which gives :- FileNotFoundError: No such file or directory. running it in IDLE 3.9, doesnt give me any error
The first thing to understand is that if the code in vscode involves paths, everything is retrieved for the root directory based on the workspace. Even if you use "." in a deep directory under the workspace, this still represents the workspace directory, instead of the current directory.
So what we need to do is to change our thinking and import the files you want with the workspace as the root directory.
This also means that ".." will not appear in vscode. Because all the files we need are in the workspace, we will not use the parent directory outside the workspace.
For example,
"./images/ui/logo.png"
If you place it like this, you can retrieve the file anywhere in the workspace without complex positioning based on the current file.
You can refer to this issue. It has similar problems with you.
I am assuming that both folders are in the same directory. This means that you should use “./“ instead of “../“ :)
I did an application in Python using Pygame, then I did the exe using cx-freeze with build_msi option. The result is that the folder where I install the application contains in its root all files needed to run (apart the libs created by cx-freeze). So running my app from the folder where is installed or clicking its link on the desktop it works. Instead if I tr to start the application from a different folder using the console it does not start because it does not find some files, but it does not search for them in the folder where they are installed... so... the problem is that I need to start the application also from a different folder and I can not use a bat file where I could change to the folder and then run the application. Is there a way so that the Python application may know where are the files without fix their path on it (because in this case I would have a lot of problems during development)? I tryed to access them trough something like os.getcwd() + fileName but it does not work anyway.
The problem here is with the working directory because if you open the file from it's folder then the working directory will be correct but if you open the file from another place the working directory will be different and you will be looking for something that doesn't exist. os.getcwd() returns the working directory. but os.path.dirname(__file__) returns the directory to the folder that the running script is in. so you can do that:
import os
projectFolder = os.path.dirname(__file__)
os.path.join(projectFolder, fileName)
I've been stuck for hours trying to figure out how I can get Pyinstaller to include images. I've made a small tkinter script which load up some images to display. The program works on my own pc, but I want to be able to download the script on another computer and run it with the pictures working, but I can't figure out how to do that.
I thought I could fix this issue by downloading a folder with the images on another machine and make the script refer to that folder but somehow it's just not working. It works when I run it on my own machine but not when I make it a .exe file with Pyinstaller.
os.chdir(sys.path[0])
path = os.path.dirname(os.path.abspath(__file__)) + r"\pics"
The thought behind the two lines above was that if I download the script on another computer as well as the pictures and insert them into a folder, I could refer to that folder and load them up (as long as the folder is in the same directory as my script) - but this isn't working. I'm clearly overseeing something, please help.
The easiest way is to use the Tree class available in a spec file. See https://pyinstaller.readthedocs.io/en/stable/advanced-topics.html#the-tree-class for the official documentation and https://stackoverflow.com/a/20677118/10475068 for an example of how to use it.
I use rtorrent on ubuntu that download folder with rar files inside.. And I want to automatically run a script that unrar, maketorrent and other things to that unrarred file. My question is.. How can i get the dirname of the directory just downloaded? It s always different so I cant use a Variable or something like that. For now, i am downloading everything to /X and extract /x/*.rar to /X2. But its a very ugly way because i have to always delete everything in /X before starting a new download or everything will mess up.
I actually have the name of the directory Which, using irssi, is stored in a variable $releasename, but thats another program, i dont think i can import it to python.. Or maybe Yes, i don't know..
I made a simple python script which creates a text file. The contents of the script are
f = open("New", "w")
f.close
Now I moved the script to the desktop (I'm on a mac) and ran it, and nothing shows up. No file is created on the desktop that is visible for me.
I actually made this little script because one of my other scripts involves opening/creating a text file and reading from it. I entered the information wrong while in my program forever, and now the entire thing is broken and I cant fix it because I have no idea where it's created the darn text file. I was under the impression that just opening a file with giving it an absolute path would create it in the same directory as the script. I seem to have been mistaken.
I've been launching the script from the terminal with the command
python3 /Users/me/Desktop/script.py
Because of that, I feel like its creating the file somewhere in the python3 install location or within wherever the python3 unix exec is located. I think. Can't check.
Are any of you guys willing to help out?
-pipsqueaker117
EDIT: Here's a link to the big program which broke.
It'll be created in the current working directory, which is the directory from which you called the script.
Your file will be created in the current directory (most probably in /Users/me/ if you just opened the terminal)
Try:
cd /Users/me/Desktop/
python3 /Users/me/Desktop/script.py
You should modify your program to change the working directory (before you write the file) to the place where you want files to show up.
import os
os.chdir('/Users/me/Desktop') # or whatever
This should be a directory where you have permission to write files.
You can always ask:
import os
cwd=os.getcwd()
print(cwd)
That will print the directory the file (without a path) will be opened in.
You can change to a specific directory (in Python) this way:
import os
try:
os.chdir('/Users/me/Desktop')
except OSError as e:
print e