Opening .py files in PyCharm Editor - python

Currently, I have PyCharm recognizing the .py extension and all .py files have the PyCharm icon. However, when I double click on a .py file, the project opens up (if it's in a project) and on the left hand side I can see everything in my project folder, but the center of PyCharm is telling me "No files are open." I can open, write, and run scratch files though.
How do I get the python script in the editor?

if you press ALT-1 you will see on the left, files of your current open project. If you click on file with .py and it does not open in the Editor window, it may mean your default layout got somehow changed. Press Shift-F12 to restore to default layout, and click on a .py file again.
Other thing to try is to delete .idea folder in the project directory and restart Pycharm.

Related

How to change directory from the Win-R Window?

After weeks of reading and experimenting, I still cannot figure out how to run Python3 programs, using batch files, from the WIN-R Run Window on Windows 10, if the batch files are saved in a different directory than the home directory.
It seems to work fine if I save both the .py file and the .bat file to the "C:\Users\Name" directory. But I would like to save my programs to Dropbox so they can be used from different PCs.
My first issue was caused by a space in the Dropbox file path. I learned that putting quotation marks around the path addresses this issue.
But I still cannot figure out how to run batch files from anywhere other than my home directory from the WIN-R Window. Is it possible to run batch files from a Dropbox folder using the WIN-R Run Window? Is it possible to change the directory in WIN-R? I've tried creating a new shortcut as suggested by others here so that WIN-R opens in my Dropbox directory, but nothing I've done to date has worked.
To change the directory from the Win-R Window, you can refer to this link:
http://www.sthda.com/english/wiki/running-rstudio-and-setting-up-your-working-directory-easy-r-programming
Hope this helps!

How do I make all .py files launch with Spyder?

I installed Spyder using Anaconda, and I am able to launch the IDE using the Spyder icon in my start menu (Win10). I wanted to set my preferences to open all .py files with Spyder, so I followed the Spyder start menu button to an executable, pythonw.exe. The problem is that I cannot launch pythonw.exe by clicking it.
How does the start menu icon for Spyder, which points to pythonw.exe, launch Spyder, but clicking the executable does not yield the same results? Also, when I double click spyder.exe in Anaconda\Scripts a command prompt opens along with the IDE, which does not happen when I click the start menu icon.
Why does this application behave so much differently than any other application I've used before (if this is just how things are in python, I apologize as I'm new!) and is it possible to set Spyder as the default application to open .py files in the same way I can open source files with IDEs in other languages?
Cheers
I found the answer in this question, answer by xyzjayne.
You create a bat file with the following contents:
start YOURPATH\Anaconda2\pythonw.exe YOURPATH\Anaconda2\cwp.py YOURPATH\Anaconda2 "YOURPATH/Anaconda2/pythonw.exe" "YOURPATH/Anaconda2/Scripts/spyder-script.py" %1
YOURPATH will be the path leading to the folder just above the Anaconda folder. For me it was:
C:\ProgramData
And you select the Choose default program to open this file... - and you choose that bat file.
When you go to Spyder shortcut's properties, the target includes a few files. So my guess is that for Spyder to run, all of these files must be run, and that's why when you just point .py files to one exe it doesn't work.
You can right click any of your *.py file, go to properties and choose Spyder as "Opens with" choice.
Right click on your file, and select open with or Choose default program to open this file.. and then in your system... select .. \Anaconda3\Scripts\spyder.exe .
I have also provided you the screenshot of the above path in my system for your reference.
In Windows 10 Anaconda installs itself into a hidden folder called ".anaconda" which is placed in the Users directory under your own profile sub directory.
When you first try to use the right-click menue "Open with" it opens up in C:\Program Files so you have to go up one folder and down into Users. You may need to have previously set one of the options in the View Menu of the file manager so that you can see hidden files. You can't do this from the "right-click open with" place, you have to set that in the regular file manager.
You will find a file called Spyder.bat a couple of folders down within that, e.g. C:\Users\Your_profile.anaconda\navigator\scripts
It will take forever to open each time.
for Anaconda3 on win11:
start YOURPATH\Anaconda3\pythonw.exe YOURPATH\Anaconda3\cwp.py YOURPATH\Anaconda3\ "YOURPATH\Anaconda3\pythonw.exe" "YOURPATH\Anaconda3\Scripts\spyder-script.py" %1
Below a baby-step guide to make Spyder your default program to open .py, including an icon!
First, search for Spyder in windows, and open the folder that contains spyder. Spyder will be a short-cut file. Right-click the spyder shortcut file and choose "Properties". Copy all the contents of the "target" content.
Open a Notepad. Write the word start and the paste the contents you obtained in step 1. Now, save the file as .bat, with any name you want, let's say "myspyderlauncher.bat", in a convenient location for you.
Go to any .py file, and choose "open with", and look for your .bat file (following the example above, the "myspyderlauncher.bat" file). Choose "always" to always open .py files with your bat.
Now double click the .py file and Spyder will automatically be started. VoilĂ !
Did you say you want a nice spyder icon too instead of that ugly white icon? Here are the next steps (optional):
Download Bat to exe converter (not worries, you will not need to install the software): https://web.archive.org/web/20190304134631/http://www.f2ko.de/en/b2e.php
Unzip the Bat_To_Exe_Converter.zip and go to the "portable" folder and run the .exe file. Open your .bat file ("myspyderlauncher.bat" in the example above), and then go to the right menu, click and look in the "icon" option and look for the Spyder icon (.ico file), which is normally in ...\Anaconda#\Scripts.
Now press "convert" in Bat_to_Exe and save your new .exe file in a convenient location with a convenient name you want. (Let's say we call it "spyderlauncher.exe")
Go to any .py file, and choose "open with", and look now for your .exe file (following the example above, the "spyderlauncher.exe" file). Choose always to always open .py files with your bat.
You will see that all your .py files are now associated to spyder, with a beautiful icon attached, and opening in Spyder.
Some screenshots below

Run a Python program at start-up on Windows

I am developing a key-logger on Python (only for curiosity sake). And the script will be an executable. The process will not need a UI or user interaction.
Is there any way, even in another executable to make the key-logger start at start-up?
I don't use Windows, but you can try making a batch script that runs your python file and make that script Run a program automatically when Windows starts:
Click the Start button Picture of the Start button , click All
Programs, right-click the Startup folder, and then click Open.
Open the location that contains the item you want to create a shortcut
to.
Right-click the item, and then click Create Shortcut. The new
shortcut appears in the same location as the original item.
Drag the shortcut into the Startup folder.
As I said, I don't use Windows, so it might be totally wrong.
You can refer here for making the BAT file, which basically says:
#echo off
python c:\somescript.py %*
pause
I think that the above answers are too complex. What I did was just drag and drop or copy and paste my file in the startup folder by clicking the quick access toolbar, typing "startup", and the job is done.
I am using Windows 10 operating system, so it might be different in your case.
I hope this is useful.
Edit: The key to this solution is to have the .py extension files open by default by the python console (not a text editor), otherwise it will just open the file instead of executing it. In order to select the default program a type of file is opened with, right click on the .py file -> Open with -> Choose default program. See this example:
Use VBScript:
1-> create anyname.vbs with this data:
Set wvbs=CreateObject("Wscript.Shell")
wvbs.run "full location of your File",0
2-> copy anyname.vbs file in C:\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup this folder
Now when windows start it will run your file in hidden mode
Open run with "Start + R", then open "shell:startup". it opens you a folder(the folder that was mentioned before at start menu), and every file that is on this folder, well run at startup.
The folder path is: "C:\Users\USERNAME\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup" (you can copy it on windows explorer, or copy this path and put your account name on USERNAME)
this is the trick i used in my script:
from os import getcwd
from shutil import copy
copy(getcwd()+'/FILE_NAME.exe','C:/Users/USERNAME/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup')
They are some ways for file name as well, but im not familiar with it. this code copies it self to startup folder and starts each time windows boots
Create a shortcut in the shell:startup folder with an absolute path to your pythonw.exe executable. The w version of Python is needed so that it starts up without a shell in the background.
Detailed instructions
Open the startup folder
Type the Windows and R keys at the same time. In the Run dialog, type shell:startup and it enter. This takes you to the Startup folder. Shorcuts in this folder are launched when the computer starts. (For me, the startup folder is at C:\Users\username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup.)
Create new shortcut
Right-click in this folder and say "New shortcut." Browse to "This PC," "Windows (C:)," "Program Files,", "Python39", and select pythonw.exe. Your Python install is likely in a different location. More recently, Python is found somewhere within %appdata%. (You can paste %appdata% into the run dialog to open this folder and look for Python. It opens in Roaming by default, but be sure to look in Local, too.)
As mentioned at the start of this answer, it is important that you select pythonw.exe rather than python.exe as pythonw.exe will run without opening a command prompt.
On the next "Create Shortcut" screen, titled, "What would you like to name the shortcut," you can name it whatever you want. For the OP, I recommend "Self penetration test."
Click "Finish."
Edit the shortcut to launch your script
In the Startup folder, right click on the app you just created and select "Properties."
In the Properties dialog's Shortcut tab, edit the "Target" to look something like "C:\Program Files\Python310\pythonw.exe" pentest_keylogger.py and the "Start in" to be the folder in which pentest_keylogger.py is found. (As above, you will need to use your Python distribution's path.)
Test your script
Hit OK. Then double-click on the icon to see if it works silently as you desire.
Finally, reboot your machine and use resmon (from the run dialog) or Ctl-Alt-Delete and the task manager to see if your app is running in the background.
Acknowledgements
Thanks to Eryk Sun and Behfar baghery for the core ideas presented here.

how to change file extensions in windows 8? i tried and it will not recognize the change

How do I change file extensions in windows 8? I tried and my system will not recognize the change.
I tried changing from .txt to .py for python so i can use in IDLE.
I created a file called myfile.txt. It showed up in Explorer as myfile.txt. If you just see myfile (no extension), then go to Folder Options, Advanced, and uncheck "Hide extensions for known file types".
I Right-clicked myfile.txt, selected Rename, and Windows selected just "myfile", not ".txt". I changed the selection to "txt", overwrote it with "py" and hit enter. Windows popped up a message warning that I was changing the extension. I clicked OK and the file was renamed.
An alternate approach is to open a command prompt, cd to the directory and use "move" to change the name.
Yet another option, if you are doing this from a text editor, click "Save As", change the save dialog's "Save As Type" drop-down to All Files, change the name to .py and hit OK. You end up with a .txt and a .py.

.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).

Categories