I am Writing The Code That You Can See Under That Article On IDLE It Correctly Works But When I Save The Code As A .py File It Doesn't Opens How I Can Open A .py File Correctly?
import random
from random import randint
random.randint(111111111, 999999999)
(Problem Solved I Erase The The Code That You Can See Under That Article And It Works)
from random import randint
If you are attempting to open a .py file by double clicking on it, you need to have the version of python you want to run it with added to your PATH. Also you need the .py file extension associated with the Python application.
The other way to do this is to have all .py files opened with Idle, Pycharm, Sublime Text, etc (Whatever IDE you sue to write it). This wont automatically run the file however, it will open it in an editable state in your IDE.
Lastly, as it relates to the code you provided, importing randint() is not necessary as you've already imported the entire random module.
Related
Every time I try to do an action such as running a program in the VScode Terminal (also happens in PyCharm) I have to enter in the full pathname instead of just the file name. For example, instead of just doing python3 test.py to run a program, I have to enter in python3 /Users/syedrishad/Desktop/Code/test.py.
Now, this is annoying and all but it doesn't bother me too much. What does bother me is when my program is trying to pull/ open files from somewhere else. If I wanted an image call Apple.jpeg, instead of just typing in Apple.jpeg, I'd have to go and find the full pathname for it. If I were to upload a piece of code doing this to someplace like GitHub, the person who'd want to test this code out for themselves will have to go in and replace each pathname with just the file name or it won't work on their computer. This problem has been going on for a while, and I sadly haven't found a solution to this. I would appreciate any help I get. I'm also on a Mac if that makes a difference.
You could use os and sys that gives you the full path to the folder of the python file.
Sys gives you the path and os gives you the possibility to merge it with the file name.
import sys, os
print(sys.path[0]) # that is the path to the directory of the python file
print(sys.path[0]+'/name.txt') #full path to the file
print(os.path.join(sys.path[0],'name.txt')) # os.path.join takes two parameters and merges them as one path using / but the line above is also fine
In VS Code, its internal terminal is in the currently opened project folder by default. Therefore, when you use the command "python file_name.py" to run the file, the terminal cannot find the file that exists in the inner folder.
Therefore, in addition to using the file path, we can also add related settings to help it find the file.
Run: When using the run button to execute the file, we can add the following settings in "settings.json", it will automatically enter the parent folder of the executed file.
"python.terminal.executeInFileDir": true,
"When executing a file in the terminal, whether to use execute in the file's directory, instead of the current open folder."
debug: For debugging code, we need to add the following settings in "launch.json":
"cwd": "${fileDirname}",
I have a list of functions stored in a file called functions.py. I will call them in the main program file. I tried all means but I do not know how to open it. When in Spyder 3, I hover over the file name in the program and would do ctrl+right click to open it. It does not work here in Spyder 4.
My code in a file called Mainfile.py
# import function file
from functions import *
(Spyder maintainer here) This is caused by a bug in Spyder 4. We plan to fix that in our next version (4.2.1), to be released in a month and a half.
I am trying to set my Python script as default program to open a file (e.g. open every .txt file with my program when I double click on it).
I already tried this:
from sys import argv
# write the arguments to a file for debugging purposes
with open("output.txt", "w+") as f:
f.write(repr(argv))
I converted the script into a .exe with pyinstaller, otherwise Windows won't let me use it to open files.
In the command prompt, it works: typing main.exe some args indeed yields an output.txt file, with inside it ["C:\...\main.exe", "some", "args"].
I was hoping that by opening a .txt file with this script (in File Explorer > right click on file > open with > more apps > check "always use this app" and selecting the executable), it would be the same as running main.exe C:\...\that_file_that_i_just_clicked.txt in the command prompt, from which I could then use the file path to open it in my program. However, this does not happen. In fact, main.exe never even gets executed (because it doesn't even create a new output.txt).
How can I link a pyinstaller-generated executable to always open a filetype, and how do I then know the path of the opened file in Python?
The thing that I was doing wrong, was creating output.txt using a relative file path. Since the script was converted into an .exe (which basically wraps the interpreter and the script into a single file), the relative file path stopped working.
Using an absolute file path fixed my issue (as pointed out by Eryk Sun).
I am using Enthought canopy for data analysis. I didn't find any option to create a .py file to write my code and save it for later use. I tried File> New >IPython Notebook, wrote my code and saved it. But the next time I opened it within Canopy editor, it wasn't editable. I need something like a Python shell where you just open a 'New Window', write all your code and press F5 to execute it. It could be saved for later use as well. Although pandas and numpy work in canopy editor, they are not recognized by Python shell (whenever I write import pandas as pd, it says no module named pandas). Please help.
Umair, ctrl + n or File > Python File will do what you want.
Best,
Jonathan
Let me add that if you need to open the file, even if it's a text file but you want to be able to run it as a Python file (or whatever language format) just look at the bottom of the Canopy window and select the language you want to use. In some cases it may default to just text. Click it and select the language you want. Once you've done that, you'll see that the run button will be active and the command appear in their respective color.
This obviously an extremely novice question, but I've installed Python 2.7 and started reading the manual. However I looked and looked, and couldn't understand how to start programming a file rather than writing in interactive mode. One book that was online suggested quit(), which surprise -- quit the program.
Should coding be done in a different program? I'm using IDLE (Python GUI). Can coding not be done within that program?
Yes, coding should be done in a different program. The interactive shell is very useful but it's not an editor.
You write Python code line by line (as you would on Python interactive mode) in a text editor such as vim, emacs etc...
Then you run these line by line code using the Python interpreter by giving it the name of your script.
$ python myscript.py
I like to use a different directory for each project. Suppose I decide to use W:/mytest as my directory. First I create the directory.
Then I start Idle. I type the following:
import os
os.chdir("W:/mytest")
This makes W:/mytest the current directory for Idle.
import sys
sys.path.append(".")
This changes the path so that when I "import", it will look in the current directory.
Next I do File / New Window to open an editor window, and in that new window I select File / Save As. It starts in the Python home directory so I have to navigate to W:/mytest. I save this (empty) file as "test1.py".
I type this into my test1.py file and save it again:
""" test1.py is my test
"""
print ("This is test1.")
class Test1:
def __init__(self):
print ("Constructed")
This is a contrived example that can be run as a script or imported as a module.
So I have two windows now; an editor window and the Idle "Python Shell". I can do this in the Python Shell:
>>> execfile("test1.py")
This is test1.
>>> import test1
This is test1
>>> tt = test1.Test1()
Constructed
Push new to start making your own script file. Then when you are ready to test click run and then you can watch the results in the interactive mode, and even try new things as if you were adding code to the end of your script file, its a very useful app for debugging, testing and trying new things.
Also in the options you can change the way python opens your scripts when you click edit from windows, you can set it so that it opens the interactive shell or just the editor.
use new window tool in the file icon,in the python idle itself to write a program
To start coding in a file, just open a new file and start typing.