PySide Dialog for files - python

I want to have the chance to choose a file or a directory, just one not both. For linux and OSX, I have the following code working:
dialog = QFileDialog(self)
dialog.setFileMode(QFileDialog.AnyFile)
dialog.setViewMode(QFileDialog.Detail)
But, on other computer, where I had installed Windows 7. It doesnot work at same. No idea. Actually, with code above, I can just choose a file, not a directory.
Any suggestion?

Related

How can I fix this issue with Windows path on Qt and Qurl?

I'm using PyQt5 and I wrote a script to download some videos and then save them into a specific directory. First I write the paths from a Linux machine, but now I'm using a pc with Windows 10 and when I open the video anything appears. Anybody knows what's the problem? I can download the videos, but it's like the path doesn't exist or something like that. There's no errors on the log.
self.mediaPlayer.setMedia(QMediaContent(QUrl.fromLocalFile(videoPath)))
I'm using this line to put the video into the mediaPlayer and everything works fine on my Linux, but when I turn to Windows nothing appears. Any idea of what could be wrong?

Python IDLE won't open with .py file, only from start menu

I've looked through many similar questions, but no one seems to have had this same problem. I can open the IDLE shell perfectly fine from the start menu. Once in, I can use File->Open to open any .py file.
However, my problem is that if I try to open any .py file from File Explorer with IDLE, the files will run, but the IDLE application won't come up. This especially makes it difficult to quit running when I can't even close the application if it's not open. It also makes it very tedious if I have to always use File->Open from the IDLE shell if I want to open a .py file. How can I at least get the Edit with IDLE option to come back?
I had this same issue and came looking for an answer, but ended up figuring it out:
I had the issue when I downloaded the 64-bit version for windows (3.9.7), but the issue was resolved when I instead installed the 32-bit version of 3.9.7.
Now I can right-click > "Edit with IDLE"
When you open the file from within IDLE, you open it in an edit window. If you try to open it from the file explorer, it will RUN it on IDLE. So that's why you can't just "open it" from the explorer.
I suggest that you start using some other tools to code in python. Sublime Text and Atom are great text editors, and both can be used as a python IDE.
The issue could be outside of Idle as I had the same behavior on OS 12.6. Restarting the Finder from the Force Quit menu resolved.

.py files not opening in IDLE from OSX Finder?

I recently re-installed Python 2.7.3 from python.org on OSX 10.8.2, and now nothing (literally nothing I can see) happens when I double-click on .py files in Finder, or when I right-click/Open-With/IDLE(2.7.3). (Edit: IDLE is already set as the default program to open .py files; the "right-click" part was just to illustrate I couldn't open them either way.)
I have Tcl/Tk 8.5.13 installed (re-installed after I re-installed Python), and I can open IDLE itself just fine, so this isn't that issue where IDLE won't work without the right version of Tcl/Tk. I can also open .py files from the Terminal using idle -e file.py without a problem.
I did notice that the Python Launcher activates when I open files this way, and is absent when I double-click on .py files in Finder (although again, literally nothing happens when I do this--I have the IDLE icon in my dock and it doesn't start bouncing or anything). Could this be a Python Launcher problem? If not, does anyone know what's going on here?
Update: I tried opening IDLE first and then double-clicking on .py files, and that works. So it seems it's just an issue of IDLE not being opened when you try to open a .py file in the Finder.
I know that means I now have at least three other ways of opening my .py files in IDLE, but I really like just being able to navigate to a file in Finder and open it when IDLE itself is not yet open. So I'd still really like to solve this problem if possible (also I really hate when stuff that's just supposed to work doesn't).
Update: Per Ned Deily's suggestion I checked for errors using Console.app and found:
3/8/13 10:42:38.006 AM com.apple.launchd.peruser.501[276]: ([0x0-0x199199].org.python.IDLE[1975]) Exited with code: 1
pops up whenever I try to double-click a .py file in Finder.
Make sure you have set the intended instance of IDLE as the default application for all .py files. One way to do so is to select a .py file in a Finder window, then select the Finder -> File -> Get Info menu item (or Cmd-I). In the Open with: section, pull down the application menu item and select the desired IDLE; if you have multiple copies of IDLE installed, select the Other.. menu item at the bottom of the list and use the file chooser to select the right version of IDLE. Then, click the Change all... button to Use this application to open all documents like this one. You should now be able to launch a .py file in the desired IDLE by double-clicking. If this doesn't work, examine system.log to see if there are any error messages being logged. You can use the Console utility to examine logs (/Applications/Utilities/Console.app).

Unusual Python exe behavior when running from Progam Files X86 versus Program FIles on Win7 is there some virtualization going on?

I have an EXE that I built that I am trying to have installed in the Program Files (x86) folder on Win7 machines. There is really interesting behavior going on. The program has a gui-we used wx-Python and XRC to handle the interface. we include an XRC folder in the distribution. The program uses getcwd() to locate itself and find the xrc folder. However, and this is really interesting-it all works fine when I place the program in Program Files. When I place the program in the Program Files x86 folder I get a message that indicates to me that it can't find the xrc folder.
Let me be clear this application runs fine on an XP machine in Program Files and on a Vista Machine as well as a Win7 machine in Program Files
Now what else is interesting is if I double click the exe in the Program Files (x86) folder it runs fine but when I try to run it off of the short cut added to the start/program menus it does not run correctly. That is where I get the error message that suggests the program is not finding the xrc folder. I also moved the program to a folder on the desktop, and at the root and in every other location it starts and runs fine. Thus I am confident that my code to point to the xrc folder is fine.
So back to the question. When programs are running from the x86 folder is something happening that causes the path references to get screwy? More importantly is it fixable?
One final note- To be as certain as possible that the problem relates to the path to the xrc folder I deleted the xrc folder from two instances of the application and I get the same error message that I received when the program was installed in the x86 folder.
Just for clarification the only time the program will not run is when I use the shortcut on the start menu and the exe and all of the parts are saved in the Program Files (x86) folder
Plasticsabers answerreally helped me understand the problem and I was able to fix it. This is not a Vista verus XP issue it is a 32 bit OS versus 64 bit issue.
I need to run on 64 bit XP and 32 bit Win7.
According to this note, the Program Files (x86) folder is specifically intended for 32-bit program files, as opposed to the Program Files folder, which is intended for 64-bit programs.
But in order for a 32-bit program to pretend that it's on a 32-bit machine, the name of the working directory has to be returned as 'Program Files,' because that's what it would be on a 32-bit machine. This is what you're seeing when you ask Windows for the working directory with getcwd(). Since your program is trying to use that directory name in a path, and that's not actually the path to the file on the real, 64-bit machine, it breaks.
Looking at the WOW64 docs, I suspect the reason it only breaks when launched from the start menu is, that's an instance of a 64-bit process creating a 32-bit process, which causes the system to change the ProgramFiles environment variable. In the case where you double-click to launch it, it's a 32-bit process from the beginning.
It seems you're not the only one to encounter this; this Windows API seems designed specifically to work around the problem you're describing. If nothing else it suggests the behavior you're seeing is by design.
But this is academic. To insulate yourself from this sort of thing, I would get the absolute path to your exe, using something like os.path.realpath(__file__), and go from there.

Default save path for Python IDLE?

Does anyone know where or how to set the default path/directory on saving python scripts prior to running?
On a Mac it wants to save them in the top level ~/Documents directory. I would like to specify a real location. Any ideas?
On OS X, if you launch IDLE.app (by double-clicking or using open(1), for example), the default directory is hardwired to ~/Documents. If you want to change the default permanently, you'll need to edit the file idlemain.py within the IDLE.app application bundle; depending on which Python(s) you have installed, it will likely be in one of:
/Applications/MacPython 2.x/IDLE.app/Contents/Resources
/Applications/MacPython 2.x/IDLE.app/Contents/Resources
/Applications/MacPorts/Python 2.x/IDLE.app/Contents/Resources
/Applications/Python 2.x/IDLE.app/Contents/Resources
/Applications/Python 3.x/IDLE.app/Contents/Resources
Edit the line:
os.chdir(os.path.expanduser('~/Documents'))
On the other hand, if you start IDLE from the command line, for example, with:
$ cd /some/directory
$ /usr/local/bin/idle
IDLE will use that current directory as the default.
I actually just discovered the easiest answer, if you use the shortcut link labeled "IDLE (Python GUI)". This is in Windows Vista, so I don't know if it'll work in other OS's.
1) Right-click "Properties".
2) Select "Shortcut" tab.
3) In "Start In", write file path (e.g. "C:\Users...").
Let me know if this works!
In Windows 10+, click the Windows Start button, then type idle, and then right-click on the IDLE desktop app and open the file location. This should bring you to the Start Menu shortcuts for Python, and you'll find a shortcut to IDLE there. Right-click on the IDLE shortcut and select properties. Set the "Start in" directory to be where you want default save path to be.
It seems like you can get idle into the directory you want if you run any module from that directory.
I had previously tried opening idlemain.py through the path browser. I was able to open and edit the file, but it seemed like I wasn't able to save my modifications.
I'm just glad to hear other people are having this problem. I just thought I was being stupid.
If you open a module, that sets the default working directory.
Start IDLE.
File -> Open to open your file. And set the current working directory.
In my case, the default directory is set to the directory from which I launched IDLE. For instance, if I launched IDLE from a directory called 'tmp' in my home directory, the default save path is set to ~/tmp. So start your IDLE like this:
~/tmp $ idle
[...]
On Windows (Vista at least, which is what I'm looking at here), shortcut icons on the desktop have a "Start in" field where you can set the directory used as the current working directory when the program starts. Changing that works for me. Anything like that on the Mac? (Starting in the desired directory from the command line works, too.)
For OS X:
Open a new finder window,then head over to applications.
Locate your Python application. (For my mac,it's Python 3.5)
Double click on it.
Right click on the IDLE icon,show package contents.
Then go into the contents folder,then resources.
Now,this is the important part:
(Note: You must be the administrator or have the administrator's password for the below to work)
Right click on the idlemain.py,Get Info.
Scroll all the way down. Make sure under the Sharing & Permissions tab,your "name"(Me) is on it with the privilege as Read & Write.
If not click on the lock symbol and unlock it.
Then add/edit yourself to have the Read & Write privilege.
Lastly,as per Ned Deily's instructions,edit the line:
os.chdir(os.path.expanduser('~/Documents'))
with your desired path and then save the changes.
Upon restarting the Python IDLE,you should find that your default Save as path to be the path you've indicated.
I am using windows 7 and by going to Start-> IDLE(Python 3.6 32-bit)
The click on properties and then in the shortcut tab go to
Start in and entering the desired path worked for me kindly note if IDLE is open and running while you do this you'll have to shut it down and restart it for this to work
If you locate the idlelib directory in your Python install, it will have a few files with the .def extension. config-main.def has instructions on where to put the custom config files. However, looking through these I did not find any configurable paths (your install may vary). Looks like you might need to crack open the editor code to alter it.
If you are using linux, you can create simple .sh file as presented below::
#!/bin/sh
cd /fullPath/PythonScripts/
idle
make the file executable by right click-> properties-> permissions-> check the execute as program checkbox-> done
Run the file :)

Categories