Using Python to generate exports for .bashrc - python

When I manually build and install projects they are installed into $HOME/.prefix/<project>
Having each project in it's own folder makes it easier to remove later. Right now I have to manually add each project to PATH, LD_LIBRARY_PATH, LIBRARY_PATH, C_INCLUDE, and CPLUS_INCLUDE. I would like to automate this using Python, but I'm running into issues. If I use Python's os.environ then the settings don't persist. I also tried outputting a single command to stdout which is run by .bashrc like this: $(python scan.py). This failed, with the following as an example:
After reloading my .bashrc my PATH variable was literally: $HOME/.prefix/clang/bin:$PATH. The problem is that last $PATH, it should have been expanded to include everything that PATH contained previously.

It sounds like your python script is outputting a string that has $HOME and $PATH in it. bash is not expanding those variables because they are not actually in your bash script.
You could write your python script to get the HOME and PATH environment variables and just output paths, rather than expecting the shell to expand them
You could pass them to your script as command-line arguments
You could eval the output in bash but eval is not necessarily a great habit

Related

Writing a command line app

I am in Ubuntu 16.4. I have just written a program that can be executed using the command(assuming i'm in the correct directory) ./Main.py -h
However I want this to be distributable, so that you can use it from the command line like nmap or youtube-dl. My idea was to put an alias is .bashrc. This had two problems. One was that I couldn't have the options(-h) in the command if there was a command after it, so it changed your directory. Second I don't know what directory the users would install it in. So even if I appended the alias to .bashrc I wouldn't know what directory it was installed in. I tried to fix this issue by copying the files to their home directory but this gave me an error that said Permission Denied(while running the program) as it needs to be able to write to files. I tryed for a while to fix this but in the end they needed to run it as root and that shouldn't be necessary. So how should I install this so that it can be used like a regular command line tool?
Normal convention for command line programs is to place them on the default path, which is to say in one of the directories represented by the $PATH environment variable. There is also a default on various systems.
This way, when you enter a command, your shell will attempt to find a match in one of these directories, and you can omit the ./ part of the invocation.
Try the command echo $PATH to see what the path directories are on your particular system. /usr/local/bin is usually a good choice for a custom script.
Since you are using Python, you will also want to set it up as an executable script, which is a pretty common thing to do.
Good luck!

Geektool not working with python3

When I try to run a python script with python3 it does not work but it works when I just use python. Why is this?
I have a simple hello.py file:
__author__ = 'A'
print("hellow")
When I use python ~/path/hello.py with geektool it works, but not with python3 ~/path/hello.py, the same works from terminal.
Also, where can I see geektool's log file?
From the comments, it looks like you have Python 3 installed at /usr/local/bin/python3. It could be that that's not part of the default PATH, but you've configured your login shell to add it to the PATH. Since your other program either executes the program directly or does it through a non-login shell, it won't read that configuration, and the PATH will remain at its default, excluding that directory. If that's the case, you might have to instead change your command to have an absolute path to Python:
/usr/local/bin/python3 /path/to/hello.py
This should work from the Terminal and any other environments.

How to execute a python command line utility from the terminal in any directory

I have just written a python script to do some batch file operations. I was wondering how i could keep it in some common path like rest of the command line utilities like cd, ls, grep etc.
What i expect is something like this to be done from any directory -
$ script.py arg1 arg2
Just put the script directory into the PATH environment variable, or alternatively put the script in a location that is already in the PATH. On Unix systems, you usually use /home/<nick>/bin for your own scripts and add that to the PATH.
Add the directory it is stored in to your PATH variable? From your prompt, I'm guessing you're using an sh-like shell and from your tags, I'm further assuming OS X. Go into your .bashrc and make the necessary changes.
An alternative approach is to create a python package with entry points and install the program, rather than changing $PATH (using setuptools in setup.py). This has some advantages:
Works outside of shells
Reduces the amount of system-wide configuration, or at least puts it all in side the package.
See Explain Python entry points? and python: simple example for a python egg with a one-file source file? for details.
This has the advantage of keeping all your settings in one place.
You can use the --develop option so that you can still edit your code in place and the --user option to avoid messing python for other users.

/bin/env: python: No such file or directory (Windows through Git Bash trying to install new Parse Cloud Code)

Trying to install python from the link here does not seem to give access to the python command in Msysgit... following the instructions here, does not actually say how to get python to work as needed.
Current error when running parse new project_name is:
/bin/env: python: No such file or directory
I believe it's likely because it installed it at C:\Python... anyone know how to fix this?
This error means that Git Bash does not know where your python.exe is. It searches your normal windows search path, the PATH environment variable. You're probably failing the 4th step on the instructions already "Make sure Python is working in the Git Bash":
$ python --version
sh.exe: python: command not found
To fix that, append C:\Python (or wherever you installed python) to your PATH environment variable in windows (instructions here). You need to restart the bash after this for the change to take effect. This will allow you to run python from the windows command prompt as well.
C:\> python --version
Python 2.7.2
If you don't want to alter your windows PATH variable or make python only available to git bash, you could create a .bashrc file in your %USERPROFILE% directory and set the variable there:
C:\>notepad %USERPROFILE%\.bashrc
and add
export PATH=/c/Python:$PATH
to the file. That script is executed every time you start the git bash and prepends C:\Python to git bash's PATH variable, leaving the system-wide PATH variable untouched.
Now that you know what has to be done, you can use this shortcut on the bash instead (appends the export command to your .bashrc)
$ echo export PATH=/c/Python:\$PATH >> ~/.bashrc
Hmmm. If you're using Python 2.7 like the instructions say to, you could try instead of that doing "C:/Python27/python.exe" insted of "python".
I think you can add the location of the python.exe in the PATH environment variable. Follow the steps: Go to My Computer->Right click->Properties->Advanced System Settings->Click Environmental Variables. Now click PATH and then click EDIT. In the variable value field, go to the end and append ';' (without quotes) and then add the absolute path of the .exe file which you want to run via Git-Bash.
don't know if this could be your issue, but its always worth a check.
check your python path is set correctly?
computer->properties->advanced system settings-> environment variables->system variables->PYTHONPATH, value = C:\PYTHON20;C:\PYTHON20\DLLS;C:\PYTHON20\LIB;C:\PY THON20\LIB\LIB-TK

python on linux: what are all the possible place where PYTHONPATH can be set?

I want to clean up my PYTHONPATH.
I know I appended it in /home/me/.profile, like so:
PYTHONPATH=/home/hoff/code/someproject/pythonmods:$PYTHONPATH
PYTHONPATH=/home/hoff/code/google_appengine/google/appengine/tools:$PYTHONPATH
export PYTHONPATH
But there must be other places I have appended the PYTHONPATH, when I go into a python interpreter and and look at sys.path, there are all sorts of additional directories.
Where might I have specified them, i.e. what are all the possible place where one can append the PYTHONPATH (on linux/ubuntu)?
sys.path and PYTHONPATH are not the same thing; the former subsumes the latter. To find out the value of PYTHONPATH, do echo $PYTHONPATH in the shell.
The files /usr/{local/,}lib/pythonX.Y/dist-packages/site.py will update sys.path so you can import packages installed with apt-get, easy_install and pip. Uninstalling these packages will trim down your sys.path (though not your PYTHONPATH).
As said before...It could be set almost anywhere. A few places that I would look (assuming your login shell is the typical bash...)
.bashrc
/etc/profile
Anything in /etc/profile.d/ (typically loaded from /etc/profile)
~/.bash_profile
~/.bash_login
from man bash
When bash is invoked as an interactive login shell, or as
a non-interactive shell with the --login option, it first
reads and executes commands from the file /etc/profile,
if that file exists. After reading that file, it looks for
~/.bash_profile, ~/.bash_login, and ~/.profile
and
When an interactive shell that is not a login shell is started,
bash reads and executes commands from ~/.bashrc, if that
file exists.
Anywhere where an environment variable can be set.
Processes inherit their parent's environment, so you can modify it at any stage, be it the shell config files, in the shell's commandline, any intermediary script you call, the python script (with sys.path) or any library it use.

Categories

Resources