I am new to python and try some simple codes using PowerShell.
I can start python by just typing py and then do codes line for line.
PS C:\Users\...\python> py
[Python starts]
>>> [I can type my code. everything is fine.]
When I try a bit more 'complex' code it is easier to write it in a .py file and run it from PowerShell, which also works fine. However, only when python is not running, i.e., I am simply within my home directory.
PS C:\Users\...\python> py game1.py
[desired output]
PS C:\Users\...\python>
The script runs fine and I get all the output, but I don't know how
to access values, classes, functions, output, etc. from the session.
Now here is my really stupid question: Can I run a .py file within a Python session in PowerShell?
Many thanks in advance!
Related
My terminal is running python 2 so when I run my python file the program fails.
So basically I am new to programming and trying to create a small python script to help me auto create folders. I also want to give the script to colleges so that they can use it on their systems too.
I know that I can run my file in terminal by using "python3 myfile.py" and it will work, but that's too much off a mission to do for my colleges and as my colleges are not familiar with code or terminal for that matter, I wanted to create an executable file so that they just click to open type a few answers to the promoted question and boom folders created.
This is where I run into a problem, I have "#!/usr/bin/env python3" at the top of my file but when I run the script IDLE opens up and it just shows the code I have written but doesn't seem to run the actual script I wrote. Am I doing something wrong?
I also then though perhaps I could just use the terminal to run the file as it is now executable, so I go into terminal and enter "myfile.py" and the program runs but in python 2 so my script fails as it is in python3. So another question would be is there a way to code into my python file, when running this file make sure you use python3? as I would want this to work on all colleges system without them having to write out anything in terminal?
Sorry for the long explanation but any advice would be greatly appreciated.
Thank you in advance
When you are on windows you can just create a .bat file where you write: python3 myfile.py in it.
Both files have to be in the same directory.
If you want to have a .exe you can also use py2exe.
You could also try just #!/usr/bin/python3 without env.
More here.
I wrote some code on my laptop and now I am trying to run this code. But it give me the output : Permission denied
Usually that should only happen if a different user would try to access it. But I am the user who wrote it and is trying to run it now. I feel like this will be an easy issue to resolve but any help would be nice! Thanks.
I wrote and saved the code in IDLE python. Once I finished I tried running it in my Terminal (I have the macOS Mojave Version 10.14.5) but then I ran into the issue.
Evandros-MBP:~ evandro$ ./Desktop/FunCodes/pwd.py
This is how I tried to call up the code. I don't see any issues here.
Instead of calling the script with
./Desktop/FunCodes/pwd.py
try using
python ./Desktop/FunCodes/pwd.py
or also
python3 ./Desktop/FunCodes/pwd.py
if you want to execute it with python3 instead of python2.
Explanation
When you write ./some-file in the terminal (without writing python before the file path), the terminal executes it as a bash file (no matter if the file has a .sh extension or not). Of course, since your file is a python file, this will fail. But even before it fails, since your python file has no permission to be executed, permission will be denied and bash won't even start.
On creation, the file only has read and write permissions. Add a shebang pointing to the Python interpreter on the script's first line, for example #!/usr/bin/env python. Also, your script is not marked as executable, in order to do that you need to use:
chmod +x pwd.py
Otherwise you will only be able to run it with Python, with
python pwd.py
I am a noob, self-motivated programmer, and had been researching methods to use my Python script to run a Powershell file that will copy and image and place the image into Excel.
I've used the subprocess, call, and Popen commands in an attempt to call and run the Powershell program from the Python script, but none has worked.
Some of the examples I found only called different functions of a Powershell script, but when I tried those settings it didn't work for my program. All of the setup for my Powershell has been established so that it can run with my PC, and also runs well when launched independently from Python.
What I would like to ask is if I had, for example, a My_program.py file and a Moving_image.ps1 file. I want to use my .py file to run/execute my .ps1 file, while both programs are located in the same path (C:\Users\Scripts).
What line of code(s), imports, and other program setup's would I need in my Python file to simply run the independent .ps1 file from my Python script?
I don't need the Powershell script to return anything to the Python script. I would like for it to simply run the copy and paste the command I sent it.
Thank you. Any type of guidance that will lead to this program actually functioning properly will be most appreciated!
Here's what worked for me (testing on linux):
python script test.py
from subprocess import call
call(["/opt/microsoft/powershell/6.0.4/pwsh", "./test.ps1"])
powershell script test.ps1
Import-Module '/home/veefu/pwshmodules/testMod'
Write-Output "Hello from test.ps1"
Get-HelloWorld
testMod.psm1 module, stored at /home/veefu/pwshmodules/testMod
function Get-HelloWorld {
Write-Output "Hello World (from Module)"
}
result when running the python script:
Hello from test.ps1
Hello World (from Module)
On windows you'll probably have to provide the complete path, C:\Windows\system32\MicrosoftPowerShell\1.0\powershell.exe and you may have to pass /file .\yourOtherScript.ps1 in the second argument to call
I'm trying out some data science tutorials with python and can't get print to, well, print! when I run a .py in windows command shell or powershell. Print does work when I use the interpreter, but I have to type it in line by line (I'm not seeing how to run a .py as a file in the interpreter). I'm attaching snips of the file, and a snip of me running in the interpreter. I tried to attach snips of what happens when I run in command shell and powershell, but apparently I need at least 10 reputation points before I can post more than 2 links. Admittedly, those snips weren't interesting; there is no error and nothing printed. It just runs and returns to the prompt.
Also, as a test, I saved a .py file that simply does print ("Hello") and it does print correctly in windows command prompt and powershell.
Thanks in advance for any help!
Casie
PY script
Snip From Python Shell
Is that image from the IDLE console? To run the script you are editing, use menu Run > Run Module, or F5. Every python GUIs has an equivalent feature.
To run an arbitrary script from inside the commandline interpreter, say mywork.py: As long as it's in the working directory that you were in when you started the interpreter, you run it (once) by typing import mywork. (As you can see, its name must be a python identifier.)
Edit: You'd get to the bottom of this a lot quicker if you'd put print("Hello, world") in a script by itself and run it. From over here it looks like it would print just fine, proving there's nothing wrong with your python interpreter.
Your script has a bug, though: As soon as you enter the function random_kid(), you leave it again since you return a value on the first line. All those print statements are never executed. Why you think it works differently with %run I can't say, but for sure this function cannot print any output.
I am using python.exe.
I tried:
C:/myfile.py
python C:/myfile.py
python "C:/myfile.py"
It always says "invalid syntax". The code is this one:
https://github.com/paulnasca/paulstretch_python/blob/master/paulstretch_stereo.py#L150
So not sure if the file has bugs or I am doing something wrong.
Your screenshot shows that you are already in the Python interpreter. Trying to run python again will result in an error. Exit the interpreter by hitting CtrlD. Make sure you have downloaded the complete paulstretch_stereo.py file. Put the file in the same directory as the files you want to process. Then, from the Windows command line, run python paulstretch_stereo.py --help and the program's options should print out.
By the way, make sure you have NumPy and SciPy installed, otherwise the program won't run.
What you get when you run python.exe directly is called the interactive interpreter.
The usual way to run a python module is simply providing it as a command-line option to the python process:
python C:/myfile.py
This command is provided from your command-line, not from the interactive interpreter.