I am very new to the language so I might need an ELI5 for the response.
I've created my ex1.py, checked all of the casing and syntax and it appears to be right?
print "Hellow World!"
print "Hello again"
print "i like typing this"
print "this is fun"
print 'Yay! printing!'
print "I'd much rather you 'n'."
print 'I "said" do not touch this!'
When I type: python ex1.py in terminal after navigating to the proper folder, the terminal displays the script, not powershell.
I've tried entering
[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27", "User")
etc. string that is on page 8 of the book into PowerShell, but that doesn't seem to help.
In the filepath above, should I be replacing C:\Python27 with the literal filepath of where i have Python installed? Likewise, "User" with my username? Or do I enter the string literally as is?
Your code is fine but it seems your environment is not set correctly. Do the following:
Start powershell
[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\<directory_where_python.exe in installed>", "User")
Restart powershell (close it, then start it back up again)
Cd in directory that contains ex1.py
Execute python .\ex1.py
Please see if you have saved the file ex1.py properly.Check once.
You may have saved the file name first and later typed in the notepad and forgot to save the file.
Related
I am trying to get a script to open the "Stellarium" application on my laptop, but it keeps giving me this error...
"Windows cannot find "stellarium.exe." Make sure you typed the name correctly, and then try again."
I am guessing something is wrong with the "prog =" syntax.
Here are files' locations...
C:\Stellarium\Stellarium.exe
C:\Stellarium\Scripts\StartUp.ssc
Double-clicking either the script file or the *.exe will open The Stellarium application as expected.
Here is the code...
stellocation = "C:\\Stellarium"
...
prog = "start /d ""+stellocation+"" stellarium.exe --startup-script startup.ssc"
os.system(prog)
sys.exit()
I think you messed up the + and the ". You effectively call
'start /d +stellocation+ stellarium.exe --startup-script startup.ssc'
Try
prog = "start /d " +stellocation+ " stellarium.exe --startup-script startup.ssc"
or even better: use subprocess.run:
import subprocess
subprocess.run(['start', '/d', stellocation, 'stellarium.exe', '--startup-script', 'startup.ssc'])
I have no windows around, so I did not try.
did you change the directory using os.chidr(location of files) first? I think that's why it can't see the exe file.
i would like to know how to send a variable from python to powershell and vice-versa:
I'm writing a code to get the infos of a install.wim file in the windows installation disk.
I want a result like this:
(python input) path = input("WIM file path: ");
(powershell background command) $wim_infos = Get-WindowsImage -ImagePath $python_input_path;
(powershell command) py .\main.py $wim_infos;
(python command) print('Your WIM file infos:');
(python output) print(sys.argv[1]);
i tried to code this but i dont know how to send the python input path variable to powershell...
sorry if I wasn't clear, I made some mistakes, or I asked for something obvious, but I'm not very good at these things...
First time ever asking for help here so my apologies if i'm missing something relevant.
I am learning Python for data science (very new), but figured I could use it to automate some of my other work.
I'll post what I have so far, but first I'll explain what I'm trying to do:
Normally (without Python) I open up the command prompt, change directories, and then paste a string (directly into command prompt) that looks like:
"program.exe C:\file path to config file\config.cfg -extra_parameters1 -extra_parameters2 -extra_parameters3 -Mail_Date=YYYY-MM-DD"
here is what I have so far:
import os
mail_date = input(("What is the mailing date?(YYYY-MM-DD): "))
os.system("start cmd.exe #cmd /k CD C:\Path")
# Next string would look like:
# "program.exe C:\file path to config file\config.cfg -extra_parameters1 -extra_parameters2 -extra_parameters3 -Mail_Date=YYYY-MM-DD
So far I can run the Python script, input a mailing date as a variable, open command prompt, then change directories. I am not sure how to then pass the "program.exe..." string to the command line.
I have created a memo.py file in the 'C: / Python' directory using the Pycharm program.
# C:/Python/memo.py
import sys
option = sys.argv[1]
memo = sys.argv[2]
print(option)
print(memo)
Where do I enter the content?
C:\Python>python memo.py -a "Life is too short"
cmd or Python interpreter or pycharm?
When you want to pass command line arguments you can run it with cmd, but you should be in the folder where file exists. Also you have to make sure python is installed it there.
You can call it like this
python memo.py "Life is too short" "one more argument"
Because you are program expect here for two arguments. or else you can try like :
python memo.py Life is
where this will consider the word after space like second argument
The o/p for above will be like :
Life is too short
one more argument
second :
Life
is
You can run the code using command line as Vikas suggested. If you really want to run the code using Pycharm, go to Run-->Edit configurations in Pycharm. Then in the "Script parameters" option write your arguments in double quotes separated by space and write -s in the "Interpreter" option. Then save the configuration and simply run the code using Run-->Run option
I have been going through "A byte of Python" to learn the syntax and methods etc...
I have just started with a simple backup script (straight from the book):
#!/usr/bin/python
# Filename: backup_ver1.py
import os
import time
# 1. The files and directories to be backed up are specified in a list.
source = ['"C:\\My Documents"', 'C:\\Code']
# Notice we had to use double quotes inside the string for names with spaces in it.
# 2. The backup must be stored in a main backup directory
target_dir = 'E:\\Backup' # Remember to change this to what you will be using
# 3. The files are backed up into a zip file.
# 4. The name of the zip archive is the current date and time
target = target_dir + os.sep + time.strftime('%Y%m%d%H%M%S') + '.zip'
# 5. We use the zip command to put the files in a zip archive
zip_command = "zip -qr {0} {1}".format(target, ' '.join(source))
# Run the backup
if os.system(zip_command) == 0:
print('Successful backup to', target)
else:
print('Backup FAILED')
Right, it fails. If I run the zip command in the terminal it works fine. I think it fails because the zip_command is never actually run. And I don't know how to run it.
Simply typing out zip_command does not work. (I am using python 3.1)
Are you sure that the Python script is seeing the same environment you have access to when you enter the command manually in the shell? It could be that zip isn't on the path when Python launches the command.
It would help us if you could format your code as code; select the code parts, and click on the "Code Sample" button in the editor toolbar. The icon looks like "101/010" and if you hold the mouse pointer over it, the yellow "tool tip" box says "Code Sample <pre></pre> Ctrl+K"
I just tried it, and if you paste code in to the StackOverflow editor, lines with '#' will be bold. So the bold lines are comments. So far so good.
Your strings seem to contain backslash characters. You will need to double each backslash, like so:
target_dir = 'E:\\Backup'
This is because Python treats the backslash specially. It introduces a "backslash escape", which lets you put a quote inside a quoted string:
single_quote = '\''
You could also use a Python "raw string", which has much simpler rules for a backslash. A raw string is introduced by r" or r' and terminated by " or ' respectively. examples:
# both of these are legal
target_dir = r"E:\Backup"
target_dir = r'E:\Backup'
The next step I recommend is to modify your script to print the command string, and just look at the string and see if it seems correct.
Another thing you can try is to make a batch file that prints out the environment variables, and have Python run that, and see what the environment looks like. Especially PATH.
Here is a suggested example:
set
echo Trying to run zip...
zip
Put those in a batch file called C:\mytest.cmd, and then have your Python code run it:
result_code = os.system("C:\\mytest.cmd")
print('Result of running mytest was code', result_code)
If it works, you will see the environment variables printed out, then it will echo "Trying to run zip...", then if zip runs it will print a message with the version number of zip and how to run it.
zip command only work in linux not for windows.. thats why it make an error..