I started learning Python this week, and I am trying to automate adding a new user to both active directory, and on Office 365.
I have managed to add the user to AD using a client and a bot, and also use another script to generate the correct New-MsolUser syntax for Powershell.
How do I get Python to open Powershell and run the output of "o365command"?
Also will I need to connect to the tenant every time I do this so will I need to incorporate this into the script as well?
Happy to show the code I have if needed.
If you provide the output from Python as JSON to a file, then PowerShell can import that directly. See ConvertFrom-Json (ConvertFrom-Json).
As for running PowerShell from Python, look at: Running an outside program (executable) in Python?
It's not something I've ever tried but good luck.
Related
I'm using VS Code for a Python project using a virtualenv. I switched my deafult terminal from powershell to cmd as VS Code was not happy executing powershell scripts.
Now when I open a terminal in my project it opens cmd (as desired), but automatically tries tor run .../Scripts/Activate.ps1, which it doesn't like. I want it to run .../Scripts/Activate.bat as we are in cmd. Runnning it manually for now, but would be nice if I didn't have to.
No doubt there is a setting somewhere to change this, but I cannot find it. Any ideas?
This is a problem related to the Python extension, it should be fixed in the last update.
You can get some information from here.
I'm trying to run a python program in the online IDE SourceLair. I've written a line of code that simply prints hello, but I am embarrassed to say I can't figure out how to RUN the program.
I have the console, web server, and terminal available on the IDE already pulled up. I just don't know how to start the program. I've tried it on Mac OSX and Chrome OS, and neither work.
I don't know if anyone has experience with this IDE, but I can hope. Thanks!!
Can I ask you why you are using SourceLair?
Well I just figured it out in about 2 mins....its the same as using any other editor for python.
All you have to do is to run it in the terminal. python (nameoffile).py
Antonis from SourceLair here.
In SourceLair, you get a fully featured terminal, plus a web server for running your Python applications.
For simple files, as you correctly found out, all you have to do is save the file and run it through your terminal, using python <your-file.py>.
If you want to run a complete web server, you can check out our server configuration guide here: https://help.sourcelair.com/webserver/configure-your-web-server/
Happy hacking!
When I code in shell for practicing and when I am done I close it, when I reopen it I cannot code in it again I have to open a new one !
How can I code in the same old one of shell ??
python command doesn't preserve your work
You can try installing IPython, Jupyter, or use a proper IDE for practicing
The python shell isn't meant to write full programs. It's nice for testing small pieces of code, but if you'd like to continue previous code, use the Python IDLE that comes with the standard download installation (in python shell -> File -> New File). This will require you to save the file to run the code. However, this IDE is not very user friendly. As user cricket_007 has mentioned, there are other IDE's that have autocomplete and other helpful tools.
Start using a terminal multiplexer like tmux or screen and launch python under it. My favourite is tmux. Your python session would persist till you kill the tmux session or till logout / reboot
I have a Python 2.7 script that among others contains the following piece of code:
import spss
columns = []
spss.StartDataStep()
dataset = spss.Dataset()
for column in dataset.varlist:
columns.append(column.name)
spss.EndDataStep()
print columns
When running this code inside a SPSS syntax (so between BEGIN PROGRAM. and END PROGRAM), it runs as expected and I end up with the variables in the active dataset.
However, when running the same code as part of a script (so from Utilities > Run script...) will return me no results.
It looks as if the SPSS session context is not taken into consideration when running a script.
Is there a way around this problem, or am I doing something wrong?
I don't want to run my code as part of Syntax file, I just want to use vanilla Python scripts.
This is, unfortunately, a complicated issue. I don't think Statistics is working as documented. I will take this up with Development.
It appears that in V24, when you run a Python script via Utilities > Run Script (which is the same as issuing the SCRIPT command), your script is connected to the Statistics Viewer process but not to the Statistics backend (the spssengine process), which is where the data live. There are typically three processes running - the stats.exe process, the spssengine process, and, for Python code, the startx process. Your script can issue commands via the spss.Submit api and can use other spss apis, but they go against a new copy of the backend, so the expected backend context is not present.
To get around this, you can run a trivial program like
begin program.
import ascript
end program.
where ascript.py is a Python module on the Python search path. (You could put these lines in an sps file and use INSERT to execute it, too.)
Another way to approach this would be to run Statistics in external mode. In that mode, you run a Python program that uses SPSS apis but the Python program is on top, and no Statistics user interface appears. You can read about this in the Python scripting help.
An advantage of external mode is that you can use your favorite Python IDE to build and debug your code. That's a big advantage if you are basically a Python person. I use Wing IDE, but any Python IDE should work. You can also set up an alternative IDE as your default by editing the clientscriptingcfg.ini file in the Statistics installation directory. See the scripting help for details. With a tool like Wing, this lets you debug your scripts or other Python code even if run within Statistics.
I'm trying to learn FreeCAD python scripting. Basically I open the python console and do what I want to do in the GUI and then look into the python console to learn the commands. and then read the API for that specific task to learn the correct form of python commands.
Things were going fine till I got stuck in this weird issue where the program (I.E FreeCAD) does not execute parts of my code. For example in this macro I create three boxes and then fuse two toghther, and it works just fine. But in this one I create 3 boxes, fuse two of them together, and then try to cut the fusion out of the bigger box. and it doesn't work. I even tried including some flags using print("flag"), but it does not execute these commands. If I copy and past the exact commands into the python consol and run it works fine!
so my speculations are:
FreeCAD does something in the GUI which does not report in the python console.
FreeCAD python interpreter does not execute some commands such as print("")
there is something in FreeCAD API which I'm not using correctly
I would appreciate if you could help me know:
if this is a bug in FreeCAD/python or it is intentional
how can I solve the issue so the FreeCAD python interpreter runs my macro/script as I expect?
P.S. I posted the exact same question here in FreeCAD forum.
OK, I figured the problem out. you may see the correct macro here in this Github Gist
explanation: Basically the solution is that when we want to run a Boolean operation on two existing objects we should not change their visibility to false (as the default GUI commands do). If we include those commands then none of the commands after them will be executed.