Naming a new file in PyCharm - python

I just started learning python. Up until now, I primarly worked with jupyter notebooks, but now wanted to try out some IDEs. So I tried out PyCharm.
So I started creating some files in a project and started trying out the commands and stuff and there is one thing which just drives me crazy, cause I can not seem to find an answer.
Everytime I create a new python file and name it something like "simple_message", it just saves as a normal file and I can't run any code in it. As soon as I remove the underscore, the file convention changes to an actual python file and I can run the code written in it. So I thought it might be the underscore that makes trouble, so I tried creating other files with underscores but all of them worked. Can someone enlighten me, why does the file name "simple_message" not work as a python file?
Thank you!
From this To this

For me I have to right click on a directory (folder) and select "New" → "Python File" to make a python file. If I go "New" → "File" it will create a text document. So long as you click the correct option you shouldn't have to worry about typing the '.py' or '.txt'
Alternatively I can right click and select "New" → "File" and then name it "simple_message.py" and it will save as a python file correctly, but I must include the .py using this method

Currently PyCharm context menu lets you choose the file type to create.
If you choose "file" you MUST provide the extension .py
If You choose "Python File" pycharm handles the extension for You.

You may need to right click in the text of your .py file and manually choose run from the menu.
Pycharm doesn’t automatically reindex when you create a new .py file, and so it probably just doesn’t know what you want it to run when you hit the green play button.
If you’re saying your python interpreter won’t run a .py file because it has an underscore in the name, that’s just not possible unless you have a modded interpreter or have done something really strange to your OS and I’m guessing you haven’t done either.
Update: it sounds like you don’t have the .py file extension on your file. This will not run for reasons that have nothing to do with Pycharm. Pycharm may “bridge the gap” for you automatically in some situations, but no reason to rely on that.

Related

How to run my code in external window (terminal)

I want my python code to be run in external terminal (window).
Obviously i have to edit 'launch.json' file, where I should
change the option 'console:internalConsole' to 'console:externalTerminal'.
Problem is, I don't find a 'launch.json' file. I guess I have
to set up one, but I'm not sure how to do it.
Seems that the extension 'code runner' could do this trick,
but the extension breaks down.
I tried to make changes in the settings menu, I chose code to be run
in external terminal, but it still uses the internal one.
May be you can give me a direction ?
Thanks
it's done.Created launch.json file and 'console: externalTerminal'

Is there a way to hide a folder even when "show hidden files" is checked using python?

I've been wanting to make a program that hides a folder that has a lot of my sensitive files on it. I use python as my main programming language.
I need a way to hide the folder from Windows Explorer, even if the "Show Hidden Files" option is checked. I know how to hide it normally, right-clicking on the folder and checking the "hidden" option, but I need users to not be able to see it at all. I also need to be able to unhide it using the same program.
If anyone knows a solution, please let me know!
Thank you!
Theoretically, you could make a program that loads your folder into the RAM on startup while deleting it and re-saving the folder on shutdown, however this would be very risky when you press "shutdown anyways". If a certain condition is met, you could show the folder(e.g. saving it to file system).
Another Idea would be to try to remake it as the very infuriating folder in C:\Program Files\WindowsApps which you can only access in command-line
Also if you are able to complete this project, I would be very interested in using it too.
I have opted for a secured, hidden folder marked as hidden, system reserved and encrypted, and requiring a python script to unlock. Using the attrib DOS command, but it does the trick. I'm also using EFS encryption on the folder, so even if the system and hidden attributes are cleared, people still can't access it.

Why do my files in vscode keep being deleted after trying to execute them?

I am currently learning to code with python in vscode but recently I have been having problems with file seemingly deleting themselves after attempted execution (image 1). I have tried Ctrl+z and I have tried recovering them but neither have worked. The files are not in my recycling bin either. If I try to create a new file of the same name in the same folder it shows me a error message (image 2) of "unable to resolve non-existing files"
When I execute my code the file saves but then becomes deleted:
This message appears if I try to undo the delete:
Have you tried to restart vscode? It is recommended that you could try the following aspects:
Check if there is any effect caused by the related expansion. For example, LaTeX Workshop extension has the function of cleaning generated files.
Check your setting.json Files,clear the contents of the user setting.json file and the vs code will return to the default value.
If it still doesn't work, you could consider installing vscode again.
Try running the Python file through a command prompt or terminal using the command python <foo_file_name>. Doing this will either confirm or deny the theory that #jasonharper made in the comments.
If you can run the script that way then we know that #jasonharper was correct and somehow your execute button/command was tampered with.
I had a similar issue using a different programming language. Building generated a file that got deleted automatically.
In my case the Windows 10 default antivirus was the cause. Turned off real-time scanning, tried again and then my files weren't deleted.
Adding an exception for the folder fixed it.
I had the Same problem with a PHP file, and turns out it wasn´t VS CODE. It was my antivirus that was moving the file to quarentine!!

I need help recognizing files in komodo in order to run them in python

I am just learning python as my first programming language, and I just installed python 3.3, 64 bit on my windows 7 OS.
I installed komodo edit 8.0, and I am trying to print ('Hello world'). I set up the correct path so that I can access python through my command prompt.
From komodo, I saved my helloworld.py file to my desktop.
When I try to run the command prompt, I search for the file, and it says file not found, or file does not exist. I can open the folder from komodo, but it appears that it is empty. When I open the folder directly from my desktop, I see the file is in there, so it seems that komodo is not recognizing it.
How can I get Komodo to recognize my saved file and run it in python? I am very new so please go step by step if you can.
Thank you!
Many of the comments you've received recommend avoiding Komodo IDE. But you're not using Komodo IDE! You are using Komodo Edit, so the comments about IDEs just don't apply.
Sure, you could use Notepad++ or even plain old Notepad, but neither of those offers any real benefit over Komodo Edit. In fact, you would be losing a valuable feature of Komodo (both IDE and Edit version): realtime syntax checking.
I use Komodo IDE and like it quite a lot. If you want to run Python programs inside Komodo and debug them right there, Komodo IDE is a great choice.
One problem may be simply that you're expecting Komodo Edit to offer the same features as Komodo IDE. It doesn't. It's just a very nice editor. You need to run your Python code outside Komodo using the command line or other means. Just open a command prompt, cd to your directory, and type python yourfilename.py.
But you say you already tried that? It must be simply a matter of being in the wrong directory.
If you have your .py file open in Komodo, do a Ctrl+O right there, and the Open File dialog will show you the file's path at the top of the dialog. You can use Alt+D to select the path, and from there you can copy it and paste it into the command line if you need to cd to that directory.
If you'd like to use an interactive debugger (an excellent idea!) without paying for Komodo IDE, you can use PythonWin.
Install Mark Hammond's Python for Windows extensions. Included in this package is PythonWin. You can open your .py file in PythonWin and select File/Debug/Step In to start debugging your code. Then you can use the other commands on the File/Debug menu to step through it. Take note of the F10/F11/etc. keyboard shortcuts which give quicker access to these commands.
Whichever way you do it, I highly recommend using an interactive debugger like this when learning a new language. Being able to stop the code and look at your variables right then and there is a huge improvement over being limited to print statements for debugging.
So I disagree quite strongly with the recommendations against using an IDE like Komodo. The very first thing I look for when I learn a new programming language is an interactive visual debugger. I don't care too much whether that debugger comes packaged as part of an "IDE" or is a standalone debugger, as long as it shows the source code, makes it easy to single step, and shows the variables whenever it's paused.
Ok heres what I personally do.
Open run, type in cmd
Navigate to whatever directory my mypythonfile.py file is
Open whatever text editor you feel like ( personally i use notepad++ because it is NOT an IDE like kodomo, but just a pretty text editor.)
Type python mypythonfile.py and hit enter. This will run the program.
Open mypythonfile.py in text editor program.
Make changes to the python file.
Go back to the cmd window and press arrow up ( to go to the last typed command) and then press enter again, to run the program again.
Repeat steps 6-7 until your program is perfect.
It seems like you are having trouble with the Kodomo IDE instead of the actual learning python process. IDEs are complicated tools with lots of buttons that are scary. Learn the language first, then once you are comfortable there, then maybe you will use an IDE? Or maybe you will just keep using a text editor instead. Thats up to you.

Default working directory for Python IDLE?

Is there a configuration file where I can set its default working directory? It currently defaults to my home directory, but I want to set it to another directory when it starts. I know I can do "import os" followed by "os.chdir("")" but that's kind of troublesome. It'd be great if there is a conf file that I can edit and change that setting, but I am unable to find it.
In particular, I've looked into my OS (Ubuntu)'s desktop entry '/usr/share/applications/idle-python3.2.desktop', which doesn't contain a conf file, but points to '/usr/lib/python3.2/idlelib/PyShell.py', which points to config-*.def conf files under the same folder, with 'config-main.def' being the most likely candidate. However I am unable to find where the default path is specified or how it can be changed.
It seems that the path is hard-coded in PyShell.py, though I could be wrong with my limited knowledge on Python. I will keep looking, but would appreciate it if somebody knows the answer on top of his or her head. Thanks in advance.
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...").
This is also my answer here: Default save path for Python IDLE? Let me know if this works!
I've found a solution after looking into PyShell.py:
Create a python file under a preferred directory, in my case '~/.idlerc/init.py', and copy/paste the following lines:
import os
os.chdir('<your preferred directory>')
Pass "-r '~/.idlerc/init.py' " argument to the IDLE startup command, like the following (your exec location and name may vary depending on OS, etc):
/usr/bin/idle-python3.2 -n -r ~/.idlerc/init.py
Just use a shell script such as:
#!/bin/bash
cd /Users/pu/Projects/L-Python
/usr/bin/idle
and run that instead of stock idle. The example is on OS X, adapt to your system.
I'm new to python and learning from 'Dive into Python' by mark Pilgrim (can be found online free)
the answer is in chapter 2.4 - hope he doesn't mind me pasting it here as its also plugging his book and is in the GPL
Before you go any further, I want to briefly mention the library
search path. Python looks in several places when you try to import a
module. Specifically, it looks in all the directories defined in
sys.path. This is just a list, and you can easily view it or modify it
with standard list methods. (You'll learn more about lists later in
this chapter.)
Example 2.4. Import Search Path
import sys
sys.path
sys.path.append('/my/new/path')
It's a good book I am a programmer - usually I find learning from books sends me quickly to sleep - not the case here ....
All I had to do here (Linux Mint 18.2 Xfce) ...
Just add path in line "working directory" = "Arbeitsverzeichnis"
It can change depending on where you installed Python. Open up IDLE, import os, then call os.getcwd() and that should tell you exactly where your IDLE is working on.
One default path is specified in idlelib.IOBinding.IOBinding.dirname or idlelib.IOBinding.IOBinding.filename
Ubuntu
So my idle-python3.desktop
file in /usr/share/applications looks like this:
[Desktop Entry]
Name=IDLE (using Python-3)
Comment=Integrated Development Environment for Python (using Python-3)
Exec=python3 -c "import idlelib.IOBinding, os; idlelib.IOBinding.IOBinding.dirname='/DEFAULT/DIRECTORY';import idlelib.idle"
Icon=/usr/share/pixmaps/python3.xpm
Terminal=false
Type=Application
Categories=Application;Development;
StartupNotify=true
To use it you need to set /DEFAULT/DIRECTORY to your desired directory, copy it with root rights into /usr/share/applications. You can also use it for Python 2 but then you need to replace the 3s with 2s.
ConfigFiles
There are also extensions that can be loaded. These must be modules and you specify them by module name. The config files for IDLE are located in HOME/.idlerc and parsed with a configparser. I did not get further with this.
Here's a way to reset IDLE's default working directory for MacOS if you launch Idle as an application by double-clicking it. You need a different solution if you launch Idle from a command line in Terminal. This solution is a permanent fix. You don't have to rechange the directory everytime you launch IDLE. I wish it were easier.
The idea is to edit a resource file inside of the IDLE package in Applications.
Start by finding the the file. In Finder, go to IDLE in Applications (in the Python folder) as if you wanted to open it. Right click and select "show package contents". Open Contents, then open Resources. In Resources, you'll see a file called idlemain.py. This file executes when you launch idle and sets, among other things, the working directory. We're going to edit that.
But before you can edit it, you need to give yourself permission to write to it. To do that, right click on the idlemain.py and select get info. Scroll to the bottom of the getinfo window and you'll see the Sharing & Permissions section. On the bottom right there's a lock icom. Click the lock and follow the prompts to unlock it. Once it's unlocked, look to the left for the + (under the list of users with permissions). Click it. That will bring up a window with a list of users you can add. Select yourself (probably the name of your computer or your user account) and click Select. You'll see yourself added to the list of names with permissions. Click where is says "Read only" next to your name and change it to "Read & Write". Be careful not to change anything else. When you're done, click the lock again to lock the changes.
Now go back to idlemain.py and open it with any text editor (you could use Idle, TextEdit, or anything. Right under the import statement at the top is the code to change the default working directory. Read the comment if you like, then replace the single line of code under the comment with
os.chdir('path of your desired working directory')
Mine looks like this:
os.chdir('/Users/MyName/Documents/Python')
Save your changes (which should work because you gave yourself permission). Next time you start Idle, you should be in your desired working directory. You can check with the following commands:
import os
os.getcwd()
This ought to be the number one answer. I have been playing around this for an hour or more and nothing worked. Paul explains this perfectly. It's just like the PATH statement in Windows. I successfully imported a module by appending my personal "PythonModules" path/dir on my Mac (starting at "/users/etc") using a simple
import xxxx command in Idle.

Categories

Resources