This question already has an answer here:
How to run Main.py automatically in PyCharm
(1 answer)
Closed 1 year ago.
Sorry, this is driving me crazy. I just started using PyCharm, and have gone through the normal steps to create a Python project. PyCharm creates a default file called "main.py". However, PyCharm does not seem to know that this is a Python file, despite the ".py" extension in its name.
The text formatting color highlights in the text editor don't work.
There is no option to Run this file.
However - when I add a new Python file manually to this project with a different name, everything works fine. However, when I create a file called "main.py", nothing works, as described above. When I create a new Python file with a different name, and then rename it to "main.py", it doesn't work again.
Why is this happening? I've searched for this problem but haven't found an applicable solution. Why does PyCharm doing something weird to files called "main.py" specifically?
Thank you.
Try Ctrl+Shift+F10,
should automatically edit the configuration.
to add to the previous answer: you can also right click on the file and choose "Run 'main'", which automatically creates a configuration with the project interpreter for that file.
Another thing you can check is your interpreter settings and ideally make sure its using venv, this way packages you install don't go into global and are kept on a project basis.
Related
This may seem trivial, and I admit that it is more a question relating to ease of access in VS Code.
My VS Code window shows "NO FOLDER OPENED". How do I fix this?
Up until the other day when I set VS Code as my default app for .py files (this is the problem), I could see any and all folders, sub-folders, and .py/.ipynb files on my Explorer tab. I am both new to Python and VS Code. I have tried these things to fix my problem:
Attempted to reset to all of microsoft's default app associations--the aforementioned files still open with VS Code.
Attempted to change each file's "Open with:" path (by accessing its properties)--it gives no other option, since PowerShell and CMD are not "apps".
Found this that got close but did not target my specific problem.
Found another article which refers to Windows 7--I didn't read it after seeing this.
I have done various other things, but I think you get the point; I'm at a loss!
This problem seems annoyingly easy to fix. I would rather see if there is some type of setting, in VS Code or Windows, that I am overlooking before searching for a complex work-around.
I'm also new to Stack overflow. I hope this is a "valid" question.
It's important to keep things organized even when you are new to coding. In VScode it is pretty simple to do that. I suggest you to make a folder for your projects and also for other purposes like practicing on examples.
Lets say you created a folder on desktop named Python, and you keep all your .py files in there, then you don't have to open each of the python files by double clicking it, it's a wrong practice. Instead, open VScode application from the start menu or desktop, then go to file > open folder > Python(the python folder you can create on your desktop). This way it opens a workspace(Folder) where you can work with different Python files or any other files.
With Ctrl key pressed, press O and then K. Then, select the folder where your python file is located.
This question already has answers here:
How to reliably open a file in the same directory as the currently running script
(9 answers)
Closed 1 year ago.
I am making a simple game with pygame, and I want it to eventually be shareable with others. However, my code manually changes the directory of the python file each time, which uses a path. I thought just putting the code in a folder in file explorer would work to change the directory, but it did not, so I used os.chdir(r'C:\Users\lukep\Downloads\Gladiator'). This works great on my computer, but it won't on anyone else's. Is there a way to make sure my program is in the same directory as the images I need without making a specific file path? Any help is welcome. Thanks!
Hard to give a definitive answer without seeing your full code, however as a rule of thumb I would say try to make use of relative paths in your code.
Also, this SO question shows how to determine which directory your script is running in:
How do you properly determine the current script directory?
os.path.dirname(os.path.abspath(__file__))
Try making a new system path variable with the respective directory address
'C:\Users\lukep\Downloads\Gladiator'.
Go to System properties → Advanced system settings → Environment variable → Path and add new path variable by pasting 'C:\Users\lukep\Downloads\Gladiator'.
I have have installed VS Code and Python 3 using brew on my mac.
Added the Python extension from Microsoft.
Works fine.
My problem is that I don't see PYTHON as a possible format in the drop down list when I am trying to do a "Save as". Only MagicPython is shown as an option.
I can force with an extension say test.py or I select the MagicPython option, but it gets saved as test.gyp
No Difference in functionality, but would like my files to get saved as .py
Is this possible?
I am having the same problems on my Mac too.
Save it as test.gyp first.
Then open it again in VS Code and go to File -> Save As.
Now it should be having a name of test.gyp, change this to test.py and click Save.
It should now be in the .py format, it worked for me.
This question could probably be closed as the mentioned issues are clearly solved in vs code Version: 1.55.0. Currently magicpython besides being default is associated to many different python file extensions - as you can see in the picture I am attaching here.
This question already has answers here:
"python" not recognized as a command
(14 answers)
Closed 2 years ago.
This is a really annoying problem. I've prowled the web for solutions, but all I found was tips about changing the PATH variable, which I did, of course. My Python install directory is C:\Python27. It' a 32 bit version. Whenever I type python in the command prompt, it says that it isn't recognised as an internal or external command. Currently, my PATH variable is set to C:\Python27;C:\Python27\Lib\site-packages\;C:\Python27\Scripts. Anyone has any ideas? I run Windows 7 by the way (64 bit). I'm pretty desperate. Heck, if nothing works I guess I'll try dual-booting Linux and Windows 7...
Just adding the Python path and trying again worked for me (without reboot).
MS-dos command for Python 2.7:
set PATH=%PATH%;C:\python27\
MS-dos command for Python 3.3:
set PATH=%PATH%;C:\python33\
(check if that is the folder where you installed Python).
Quick fix: May not be the most elegant or long term fix but if you are really frustrated and just want to get it to run, just copy paste the python.exe file to your current directory. This worked for me.
After adding the python folder to the system PATH variable, you should reboot your computer.
Another simple solution is: create a shortcut of the python.exe executable (probably it is in C:\Python27\python.exe, or similar) in a place like C:\Windows\system32 (that is, a place that already is listed in the PATH variable). The name of your shortcut should be python (maybe python.exe should work too). I mean, it can't be python - shortcut or similar, for your purposes.
To see the contents of the PATH variable, go to the cmd and enter set PATH.
After changing the PATH variable in windows, you need to reboot your system before it takes effect.
Edit: As stated by #tdelaney, only a restart of cmd.exe should be required. This is true atleast for Windows 7 64bit.
In your PATH have you got a leading space before your Python path?
For example:
"C:\somedirectory\bin; C:\Python27;C:\Python27\Lib\site-packages\;C:\Python27\Scripts"
If you have a leading space between path's "ry\bin; C:\Pyt", it won't work and is usually the cause for this type of issue.
When installing, there is a checkbox that is by default not selected, but it asks to add python to the environment variable. Re-install and check that box.
I'd rather the installer do it than struggle in the weeds myself.
Okay, as you said your Python install directory is C:\Python27, open my computer, then open c: drive,
if you don't see "Python27" named folder there then try to search it using search option, (in my case i found it in old.window folder, don't know how it moved there) cut and past it in c drive along with folders like, program files, user etc... , now open cmd and type python and hit enter to check if it is working now,
This is only a partial answer, but I found (repeatedly) that I'd have similar issues when I would use the gui installer and not go through the custom setup.
Using the custom setup option, then using the same settings, the "install for all users" (that then installs to C://python.version/blah instead of the user based default structure) WOULD allow the installer to setup PATH correctly.
Yes, I've searched. So after spending about 4-5 hours struggling just to get Python files running, I recently stumbled over the solution to get it running through the environment variables like this: cmd -> python -> Python starts, yay yay
Since it didn't work to do it through the command line and similar I had to do it manually through the Windows interface. Now that it's working, however I cannot open .py files without typing out the full path like this: python C:\X\X\X\test.py which is obviously also starting to get annoying.
So now I'm trying to find out which variable I have to change (yet again) to only be able to type 'python test.py' and have it running. Sorry if I come off vague, but it's always a major pain to setup a new programming language for me and it kills my mood.
Thanks for help, it'll be really appreciated.
When you say
able to type 'python test.py'
I'm not sure exactly what you mean. Normally when the Python interpreter runs, it looks in the current directory for any source file that is named on the command line (unless you specifically name a location for the source file, as you've discovered). It seems from your previous statement:
python C:\X\X\X\test.py which is obviously also starting to get annoying
that your test.py file exists somewhere else.
What you might want to try is to change the current directory first, before running your script. In a command prompt window, type:
C:
cd \X\X\X
python test.py
(obviously substituting your actual path name). My apologies if you already know this.
To make python executable on your command line, you need to add it to your PATH environment variable, which it sounds like you have done on the command line. It is quite simple to add directories to the PATH in Windows if you know where to look. Essentially, you need to get to the Environment Variables dialog box, which is slightly different for each version of Windows.
For Windows XP: Start -> Control Panel -> System -> Advanced -> Environment Variables
For Windows Vista, 7: Click the Start Orb, right-click Computer and select Properties -> Advanced -> Environment Variables
Then, in the lower of the two boxes, find Path and click Edit. Change it so that C:\Python27 (or whichever version of Python you have) is at one end of the list, separated from the other entries by a semicolon (e.g. C:\Python27;C:\Program Files ...)
Once you've done this, python will work at the command line whenever you open a command window.
Regarding your second issue, however, there isn't much you can do. You must either specify the complete path to your script or already be in the same directory as the script. That is, if the script is in C:\X\X\X you will either need to invoke it as C:\X\X\X\test.py or first cd C:\X\X\X.