How do I run a downloaded repository's config in Python? - python

I am trying to use sunnyportal-py. Relatively new to python, I do not understand step 2 in the README:
How to run
Clone or download the repository.
Enter the directory and run:
PYTHONPATH=. ./bin/sunnyportal2pvoutput --dry-run sunnyportal.config
Enter the requested information and verify that the script is able to connect to Sunny Portal.
The information is saved in sunnyportal.config and can be edited/deleted if you misstype anything.
Once it works, replace --dry-run with e.g. --output to upload the last seven days output data to pvoutput or --status to upload data for the current day.
Add --quiet to silence the output.
Could anyone help me? I have gone into a cmd.exe in the folder I have downloaded, I don't know how to correctly write the python path in the correct location. What should I paste into the command line? Thanks!
Edit : I would like to be able to do this on Windows, do tell me if this is possible.

The command at bullet 2 is to be typed at the commandline (You need to be in windows: cmd or powershell, Linux: bash, etc.. to be able to do this).
PYTHONPATH=. ./bin/sunnyportal2pvoutput --dry-run sunnyportal.config
The first part of the command code above indicates where your program is located. Go to the specific folder via commandline (windows: cd:... ; where .. is your foldername) and type the command.
The second part is the command to be executed. Its behind the "--" dashes. The program knows what to do. In this case:
--dry-run sunnyportal.config
running a validation/config file to see if the program code itself works; as indicated by "dry run".
In your case type at the location (while in cmd):
"sunnyportal2pvoutput --dry-run sunnyportal.config"
or
"sunnyportal2pvoutput.py --dry-run sunnyportal.config" (without the environment variables (python path) set).
Note: the pythonpath is an environment variable. This can be added via: Control Panel\All Control Panel Items\System\ --> bullet Advanced System Settings --> button "environment variables". Then you can select to add it to "Variables for user "username"" or "system variables". Remember to reboot thereafter to make the change effective immediately.
Update 1 (pip install sunnyportal):
go to cmd.
type "pip search sunnyportal"
Result:
Microsoft Windows [Version 10.0.18363.836] (c) 2019 Microsoft
Corporation. All rights reserved.
C:\Windows\System32>pip search
sunnyportal
sunnyportal-py (0.0.4) - A Python client for SMA sunny portal
C:\Windows\System32>
If found, then type:
"pip install sunnyportal"

Related

how to set up environment variables in mac terminal and have python scripts pick the variable up effectively?

I'm using MacOS and working on a python project.
In my project there is a line:
num_workers = int(os.environ.get('NUM_SAS_WORKERS', 1))
I want to add NUM_SAS_WORKERS in my environment variables and set it to 10 so that my python project can load the value 10 into num_workers
I've tried executing this in my terminal:
export NUM_SAS_WORKERS=10
and I was able to verify that NUM_SAS_WORKERS = 10 exists by running printenv in terminal
But it doesn't work. In my python script num_workers is still loaded with 1
How to do it correctly?
If you want to access the NUM_SAS_WORKERS environment variable in the Python shell, run this command from your terminal:
NUM_SAS_WORKERS=10 python
And once in the Python shell:
>>> import os
>>> int(os.environ.get('NUM_SAS_WORKERS', 1))
10
If you want to access it in a file, very similar:
NUM_SAS_WORKERS=10 python yourfile.py
If you set up environment variables from the terminal command it gets erased as soon as you close that terminal. To set up and keep them you have to update them in .bash_profile file which resides in the home directory as a hidden file. Follow the below commands to update it.
Open terminal
cd ~
(To take you to the home directory of the mac)
open -a "Visual Studio Code" .bash_profile
( To open .bash_profile file, in the place of "Visual Studio Code" you can use any text editor name)
Now, .bash_profile will be opened and there you can set your environment variables and save it.
Example:
NUM_SAS_WORKERS=10
Now run echo $[NUM_SAS_WORKERS] in terminal to read that
After performing the above steps some times still environment variables not available to read in your python project/file. In such cases, you have to restart or log off and log in to your machine that can fix the issue.
To check whether the environment variable is available to your python code run the below script in your python console. This should print all your environment variables.
import os
print(os.eviron)

most of terminal commands do not work

I am new to python and OSX terminal commands. I wanted to permanently add a directory to PYTHONPATH, thus I looked here and used these instructions:
1-Open up Terminal
2-Type open .bash_profile
3-In the text file that pops up, add this line at the end: export PYTHONPATH=$PYTHONPATH:foo/bar
4-Save the file, restart the Terminal, and you're done
I also followed this instruction:
export PYTHONPATH="${PYTHONPATH}:/my/other/path"
After these changes I cannot use any command like python, which, whereis, pip, etc. in Terminal :(
The response is something like below:
-bash: which: command not found
I do not know what I have done.
are you sure you have appended the path because what i can see is you have replaced old path definition and assigned a new one
remove bash profile use one of these commands /bin/rm ~/.bash_profile Or if you want to save your current bash configs then use a mv instead /bin/mv ~/.bash_profile ~/bash_profile Then close your terminal and start again you will be able to use all commands and apps

How to setup Atom's script to run Python 3.x scripts? May the combination with Windows 7 Pro x64 be the issue?

I'm trying to switch from Notepad++ to Atom, but I just can't manage to get my scripts executed in Atom.
I followed this answer (so I already installed script) which is not really extensive and also the rest on the web doesn't offer anything comprehensible for beginners.
In Notepad++ NPPexec I used to
NPP_SAVE
cd "$(FULL_CURRENT_PATH)"
C:\Python34\python.exe -u "$(FULL_CURRENT_PATH)"
and in Sublime Text 2 I made it run by creating a new "Build System":
{
"cmd": ["C:\\python34\\python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
Can you please guide me how to setup Atom to be able to execute Python scripts with Python 3.4 scripts with a keyboard short-cut?
I already tried to set my init-script to:
process.env.path = ["C:\Python34\python.exe",process.env.PATH].join(";")
respectively
process.env.path = ["C:\Python34",process.env.PATH].join(";")
with no success.
When I go to Packages -> Script -> Configure Script and type
C:\\Python34\\python.exe
it works. But thats not a permanent solution.
When I press Ctrl+Shift+B to run a script, without configuring it before (as it is supposed to work), I get (suggestion of ig0774's comment implemented):
(it doesn't matter whether it is C:\Python34 or C:\Python34\)
It complains that python is not in my path - but it is.
I read multiple times that Windows 7/8 64bit together with Python 3.x could cause issues with certain packages. May this be the reason in ths case as well? I have Windows 7 Pro x64.
Update
As I've switched to VSCode and probably stay there, I'm not willing/don't have the time to try out all the answers, so I let the community judge the answers and accept always the highest voted. Please ping me, if it's not correct anymore.
This can be easily solved by editing the /home/.atom/packages/script/lib/grammars.coffee file (note that the atom folder is hidden so you might have to press ctrl+H to view hidden files and folders)
Inside grammars.coffee find:
Python:
"Selection Based":
command: "python"
args: (context) -> ['-u', '-c', context.getCode()]
"File Based":
command: "python"
args: (context) -> ['-u', context.filepath]
and replace with:
Python:
"Selection Based":
command: "python3"
args: (context) -> ['-u', '-c', context.getCode()]
"File Based":
command: "python3"
args: (context) -> ['-u', context.filepath]
Save changes, restart Atom and enjoy running your scripts with python 3
EDIT: On Windows I believe the grammars.coffee file is located in
C:/Users/Your_Username/AppData/Local/atom/packages
Again, the AppData folder is hidden so you might have to change your settings to view hidden files and folders.
To expand on #matt-nona answer. You can go to his mentioned config file right from Atom. Simply go to settings then "Open Config Folder":
Then /packages/script/lib/grammars.coffee Find "Python" and make the appropriate change to python3:
Following up on Matt Nona's advice , when Atom starts-> Welcome Guide (or control+shift+T)-> 5th one down 'Hack on the Init Script'. A blank page will open and you can add that modifications in there.
Update: for any other souls looking for this answer - On my Mac I do not have a grammars.coffee file within atom script config file.
Instead, there s a grammars folder, and I have a python.coffee file in there. The same changes outlines in the screenshot (ie add '3' to the end of the two mentions of python) fixed my issue and atom automatically runs Python3 now.
Not sure if the above answers are Windows specific or if there have been dev changes since 2017.
same problem just like you. 'Packages -> Script -> Configure Script' is not permanent. So I has tryed another script runner:https://atom.io/packages/atom-runner, just found the problem is in the python script itself.
When I use atom-runner, I got error message like this:
atom-runner error
So it remind me that in the beginning of the python script: ' #!/usr/bin/env python3 '
It's obvious that the ENV_PATH is WRONG here. I should revise it in my python script.
Setting the PATH within Atom did not work, setting it with the cmd, via
set PATH=%PATH%;C:\Python34
neither, and setting it in the Windows 7 system properties failed as well.
However reinstalling Python 3.4 and check Add python.exe to Path
seems to be neccesary. Also I needed to uninstall Atom completely (inculding all packages or a least script) and reinstall it from scratch.
After all these steps:
Install Python with Add to Path
Install Atom
Install script package
it works out of the box (Ctrl+Shift+B) and no further steps are required.
I still don't know what was the reason before and I don't know which of this steps are really required. So feel free to include your procedure without reinstalling everything.
Update
Reinstalling everything is certainly not necessary, simply updating/repairing the installation with the installer is sufficient.
Use the script-runner https://atom.io/packages/script-runner/
"N.B. these keyboard shortcuts are currently being reviewed, input is welcome.
Command Mac OS X Linux/Windows
Run: Script ctrl-x alt-x
Run: Terminate ctrl-c alt-c"
And "Run Terminate" (Alt + c) to use the current python in your system.
edit your python.coffee script
$ sudo nano .atom/packages/script/lib/grammars/python.coffee
For Linux and Mac, adding environment in the script will pick correct python version. (command + I to run)
for running with python3
#!/usr/bin/env python3

unable to install PunJab

I am unable to install PunJab on my system.
PunJab is a HTTP jabber client
interface. It is a BOSH connection
manager that allows persistent client
connections to a XMPP server.
When I type the following command
python setup.py install
It says invalid syntax error.. I have python 2.7 and twisted python 10.2 running on windows XP.I have set the environment variables for python...
What have I done wrong ?
Thanks
wat the heck is punjab?? I have no clue what it is.. But anyways when you add it to your system path it doesnt become part of your system path immediately.. A restart is normally required.
Instead what I would suggest is go to your python install directory and find the executable or python script that is needed to run this program and go to the command prompt and run it with your file name.
Going to Cmd prompt: Start -> Run -> type cmd and Enter
A black box should appear and now type the executable full path that you need to use like
C:\Python27\python.exe "location of your file"
thus your command should look like
C:\Python27\python.exe C:\users\kavya\Desktop\Punjab.py
Well hope that helps and please let me know wat the heck is Punjab and I know u r nt talking abt the state..

starting Python IDLE from command line to edit scripts

I've tried many variations of this command: idle.py -e filepath, but it simply starts IDLE like normal, not opening any extra windows for editing, and not throwing any errors.
So how can I do the equivalent of opening IDLE, file>open>filepath via the command line (or perhaps even a Python module)?
You need to do as stated in the main.py file of the idelib folder (C:\Python33\Lib\idlelib), at least on the python 3.3 version explains that:
IDLE main entry point
Run IDLE as python -m idlelib
So with python -m idlelib <script_to_edit> you will be able to open and edit the script with idle. I haven't checked with previous versions but it could be the same comand
This is also documented on the changelog of the version 3.3.3
Make a new text file, and put something like this in it:
C:\Python26\Lib\idlelib\idle.pyw "C:\file1.py" "C:\file2.py"
In your actual script, you'll replace "C:\file1.py" and "C:\file2.py" with your files' paths, save as a .bat, and then launch it. That should do what you want.
Please forgive me for bumping such an old thread, but I've been teaching myself linux and python with the help of the community, and was trying to figure out how to invoke IDLE2 and IDLE3 from the command line. I came across this post some of the solutions seemed a bit complicated for my application. Then it occurred to me that I could just put syslinks in the /usr/bin/ path for each.
sudo ln -s idle-python3.1 idle3
sudo ln -s idle-python2.6 idle2
to address the OP. From the directory the script is located, type:
idle3 abc123.py
or
idle2 abc123.py
I'm just so damned happy that I finally had a "light bulb" go off that I wasn't going to let a 2 year old post stop me from posting my solution.
Rarely the native os is useful. I created a 'win batch file, in the folder with my .py files:
start /MIN cmd /C c:\Python27\lib\idlelib\idle.py -e %1 %2 %3 %4 %5 %6
This can open up to six files from cmd line in one shot. Just type the name of the batch file, followed by from zero to six filenames. Also if one or more files you specify are not found, idle opens these as new document(s).
first make sure you have location of idle in path
I am using "python3.5".So mine looks like this:
C:\Program Files\Python35\Lib\idlelib.Yours may differ.
use this following command:idle -r file_name.py to run the file
or just idle file_name.py to edit
or start idle -r file_name.py ^&exit
you can just program in Python to edit your Python files. A simple example. say you want to search for a word and then replace it with something else.
import fileinput
import os
os.chdir( os.path.join("c:\\","path") )
for file in os.listdir("."):
for line in fileinput.input(file,inplace=0):
if "search word" in line :
line=line.replace("search word","new word")
print line
(use inplace=1 to do in place editing.). Then save and run the script as normal Python script using the interpreter.
Just add IDLE's path to your PATH environment variable.
For example I created an environment variable called IDLE_PATH and set the value to C:\Python27\Lib\idlelib
Then in my PATH variable I added ;%IDLE_PATH%; and open a new cmd prompt or in console2 just open a new tab and run idle <file_name> to open the file, you will be able to do this from any directory. In IPython console add an ! before the command, for example !idle test.py.
Congrates, Now you're a python pimp!
paste the idlelib to your system path or user path, environment variable.for example, like this
C:\Program Files\Python310\Lib\idlelib
then type idle in your command prompt. done.

Categories