Here's my directory structure (creddump/ is a program I downloaded directly from github that is not supported by any package manager):
/usr/local/bin/
---> creddump/
---> pwdump.py (imports from framework)
---> framework/
---> types.py
---> lsadump.py
---> another_program/
---> x.py
I'd like to be able to run pwdump.py and x.py simply by typing pwdump.py and x.py, not their full paths. So, I added their paths to my $PATH environment variable by adding it to the beginning of this line in the ~/.bashrc file like this:
export PATH=/usr/local/bin/creddump/:/usr/local/bin/another_program/:~/.local/bin:/snap/bin:/usr/sandbox/:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/usr/share/games:/usr/local/sbin:/usr/sbin:/sbin:$PATH
That's great, but I don't want to manually add another line to $PATH every time I want to download another program. I did some digging and found out that bash supports wildcard expansion. I uncommented this line in ~/.bashrc: shopt -s globstar and I added /usr/local/bin/**/: to the beginning of the export PATH line above. After, I did source ~/.bashrc and/or I restarted the terminal during my various attempts. But, it didn't work;
bash: pwdump.py: command not found
I also tried adding it to the middle and end, but nothing worked.
I know this should be able to work because I've gotten it to work before I upgraded my Parrot Security OS system from 4.10 to 4.11. I've even tried adding the export PATH line to ~/.profile as well.
Any ideas on how to fix this? Thank you so much!
For whom tommy2111111111's incredibly simple solution (for which I can't find an explanation) doesn't work, here's an alternative in accordance with documentation:
d=(/usr/local/bin/**/)
PATH="${d[*]/%\//:}"$PATH
so when I experiment on my own system, this is how I see the path variable once I added the globstar pattern: ...:./path1:./**/ - which is not reading all the paths (perhaps pattern matching is not part of PATH, dont know)
If you are not creating new subdirectories all the time and just want "all subdirectories that currently exist to PATH variable", then a for loop to upgrade your PATH variable would suffice (like is mentioned here: https://unix.stackexchange.com/questions/17715/how-can-i-set-all-subdirectories-of-a-directory-into-path/17856#17856)
but if you want to achieve objective "add all future subdirectories to my PATH"; then in that same answer link, there is a mention of http://www.gnu.org/software/stow/ which seems like its solving your usecase
OMG!!! I fixed it! Instead of adding /usr/local/bin/**/, I just did /usr/local/bin/** without the ending /. I also had to edit the ~/.bashrc file with sudo nano ~/.bashrc. Thank you to everyone else who responded; your solutions work perfectly for me, but I was just looking for a much simpler solution.
You can try something like this. Just name the file file.txt
add_path() {
declare -a new_path
local IFS=:
mapfile -t new_path < <(find /usr/local/bin/ -type f -name '*.py')
NEW_PATH=${new_path[*]%/*}
export PATH="$PATH:$NEW_PATH"
}
add_path
Just source that file.
source ./file.txt
Then try to execute your python scripts without the absolute paths.
Related
dag#Arokh:~$ source /home/dag/.bashrc
dag#Arokh:~$ echo $PATH
/home/dag/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
dag#Arokh:~$ python3 /home/dag/.local/bin/facemorpher --version
Face Morpher 1.0
dag#Arokh:~$ python3 facemorpher --version
python3: can't open file 'facemorpher': [Errno 2] No such file or directory
Adding the directory to PATH doesn't seem to help. How can I make python3 facemorpher work from any directory?
python3 pays no attention to the PATH variable when looking for scripts. This variable controls where the shell looks for executables.
So what should work now, provided the script has executable permissions and a valid shebang like #!/usr/bin/env python3 as its very first line, is that simply
facemorpher
without python3 in front should run the script. Perhaps this is actually what you want and need.
The python3 command without any options expects a file name parameter; there is no simple way to make it look for a file which doesn't exist in the specified directory (which is the current working directory if the filename doesn't have an explicit directory part; this is how the operating system resolves relative file names, and should probably not be messed with for an individual command). For further details about this, perhaps see also Difference between ./ and ~/
For the record, chmod a+x path/to/scriptname adds execute permission to the script for all users on the system, and the path in the shebang after #! should point to your Python interpreter's full path, or the full path to a utility like env which finds it on your PATH and executes it based on just the command name (here, python3; but on some systems, the Python interpreter executable's file name is just python, or more broadly whatever the system's owner decided to name it).
My Mac has contained multiple python versions so that I would like to fix one as my default python.
In the current, it has set the path of anaconda python. I have stopped to use the anaconda-python.
And I would like to change the python3 as default following with path:
/usr/local/bin/python3
How can I do this?
Hope:
python3 as default that path
jupyter notebook would follow that path
pip3 install package would follow that path
Update your environment variable PATH to include your required python path. Append the python path in the beginning of the environment variable PATH, so that first it'll look in that path.
You need to update that in .bash_profile file. For that go to the root path and look for the mentioned file. Run ls-a to check whether the file is present.
cd ~
ls -a
Open the file
vi .bash_profile
Add below commands in the file (press i to go into insert mode)
# Setting PATH for Python
export PATH={ur path}:$PATH
(Save the file by pressing :wq)
Check whether the update is made by running
echo $PATH
You can change the path to this by editing the .bashrc file
export PATH="/usr/local/bin/python3:${PATH}"
I'm trying to import a module for python that I have written that is contained in a Dropbox folder whose path contains a space. Following the comments here, I don't want to do a sys.path.append(path_to_repository) every time I use python, I'd rather just update my bash profile to point to the correct Dropbox folder once.
I've tried adapting the code from the previous page by appending the following lines to my ~/.bash_profile:
PYTHONPATH ="/Users/myusername/Dropbox (projectname)/REPOSITORY_NAME"
export ${PYTHONPATH}
When I close the terminal window and reopen, I get the following error message:
-bash: PYTHONPATH: command not found
-bash: export: `/Users/myusername/Dropbox': not a valid identifier
-bash: export: `(projectname)/REPOSITORY_NAME': not a valid identifier
and (not suprisingly) when I then try to import from the repository in python, I get a module not found:
>>> from REPOSITORY_NAME import myfile
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named REPOSITORY_NAME
Does anyone have any solutions?
Some questions/possibilities I'm thinking of -
1) Does it have anything to do with my Anaconda configuration? (Anaconda runs in a virtual env)
2) Does it has anything to do with the folder with python code being installed in Dropbox?
3) Could it be that spaces in pythonpath are not interpreted correctly?
4) Is there any problem with this being the same directory that syncs to github and bitbucket?
Thanks in advance for your help.
*Edit:
The solution seems to be to 1) erase the extra space in the first line and 2) repalce the ${PYTHONPATH} with PYTHONPATH in the second line, i.e. to adjust ~/.bash_profile to have the following line:
PYTHONPATH="/Users/myusername/Dropbox (projectname)/REPOSITORY_NAME"
export PYTHONPATH
I had a similar problem a while ago. The error message is indicating that the problem lies in the space in the directory path - the bash_profile is being truncated by the space and it is splitting the path into 2. It may be a problem with the way bash handles spaces, but I am not 100% sure.
Here is one solution that worked for me:
export PYTHONPATH="/Users/myusername/Dropbox (projectname)/REPOSITORY_NAME"
It is similar to what you have, but export and PYTHONPATH are in the same line. I don't think this would interfere with Dropbox, Github, Bitbucket, Anaconda (or any other virtualenv like Enthought) etc., as long as you as have a _init__.py' file in each directory where you have your .py files.
Hope this helps
Yeah, you put a space before the = sign when exporting PYTHONPATH:
PYTHONPATH ="/Users/myusername/Dropbox (projectname)/REPOSITORY_NAME"
Should be:
PYTHONPATH="/Users/myusername/Dropbox (projectname)/REPOSITORY_NAME"
What file do I edit, and how? I created a virtual environment.
The most elegant solution to this problem is here.
Original answer remains, but this is a messy solution:
If you want to change the PYTHONPATH used in a virtualenv, you can add the following line to your virtualenv's bin/activate file:
export PYTHONPATH="/the/path/you/want"
This way, the new PYTHONPATH will be set each time you use this virtualenv.
EDIT: (to answer #RamRachum's comment)
To have it restored to its original value on deactivate, you could add
export OLD_PYTHONPATH="$PYTHONPATH"
before the previously mentioned line, and add the following line to your bin/postdeactivate script.
export PYTHONPATH="$OLD_PYTHONPATH"
The comment by #s29 should be an answer:
One way to add a directory to the virtual environment is to install virtualenvwrapper (which is useful for many things) and then do
mkvirtualenv myenv
workon myenv
add2virtualenv . #for current directory
add2virtualenv ~/my/path
If you want to remove these path edit the file myenvhomedir/lib/python2.7/site-packages/_virtualenv_path_extensions.pth
Documentation on virtualenvwrapper can be found at http://virtualenvwrapper.readthedocs.org/en/latest/
Specific documentation on this feature can be found at
http://virtualenvwrapper.readthedocs.org/en/latest/command_ref.html?highlight=add2virtualenv
You can create a .pth file that contains the directory to search for, and place it in the {venv-root}/lib/{python-version}/site-packages directory. E.g.:
cd $(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")
echo /some/library/path > some-library.pth
The effect is the same as adding /some/library/path to sys.path, and remain local to the virtualenv setup.
Initialize your virtualenv
cd venv
source bin/activate
Just set or change your python path by entering command following:
export PYTHONPATH='/home/django/srmvenv/lib/python3.4'
for checking python path enter in python:
python
\>\> import sys
\>\> sys.path
I modified my activate script to source the file .virtualenvrc, if it exists in the current directory, and to save/restore PYTHONPATH on activate/deactivate.
You can find the patched activate script here.. It's a drop-in replacement for the activate script created by virtualenv 1.11.6.
Then I added something like this to my .virtualenvrc:
export PYTHONPATH="${PYTHONPATH:+$PYTHONPATH:}/some/library/path"
It's already answered here -> Is my virtual environment (python) causing my PYTHONPATH to break?
UNIX/LINUX
Add "export PYTHONPATH=/usr/local/lib/python2.0" this to ~/.bashrc file and source it by typing "source ~/.bashrc" OR ". ~/.bashrc".
WINDOWS XP
1) Go to the Control panel
2) Double click System
3) Go to the Advanced tab
4) Click on Environment Variables
In the System Variables window, check if you have a variable named PYTHONPATH. If you have one already, check that it points to the right directories. If you don't have one already, click the New button and create it.
PYTHON CODE
Alternatively, you can also do below your code:-
import sys
sys.path.append("/home/me/mypy")
I'm on Mac OS 10.6 Snow Leopard and I'm trying to add a directory to my PATH variable so I can run a tiny script I wrote by just typing: python alarm.py at the terminal prompt.
I put the path in my .profile file and it seems to show up when I echo $PATH, but python still can't find script the that I've put in that directory.
Here's the contents of my .profile file in my home directory:
~ toby$ vim .profile
export PATH=/Users/tobylieven/Documents/my_scripts:$PATH
Here's the output of echo $PATH, where all seems well:
~ toby$ echo $PATH
/Users/tobylieven/Documents/my_scripts:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
Here's the script I'm trying to run:
~ toby$ ls /Users/tobylieven/Documents/my_scripts
-rwxrwxrwx# 1 tobylieven staff 276 17 Jan 21:17 alarm.py
Here's the command I'm trying to use to run the script and the fail message I'm getting instead:
~ toby$ python alarm.py
python: can't open file 'alarm.py': [Errno 2] No such file or directory
If anyone has an idea what I might be doing wrong, that'd be great.
Thanks a lot.
PATH is only for executables, not for python scripts. Add the following to the beginning of your Python script:
#!/usr/bin/env python
and run
sudo chmod a+x /Users/tobylieven/Documents/my_scripts/alarm.py
Then, you can type just alarm.py to execute your program.
Which python are you targeting?
Did you install it with brew? It uses a different path.
which python3 or which python
Choose the one you want
Copy that output
Paste it at the top of your python file
add a #! in front of that path so it looks something like
#!/usr/local/bin/python3
Make sure to change the file permissions
chmod +x filename
Put that file in a folder that is in your path
Not sure if your folder is in your path?
echo $path
How to add that folder to your path?
Find your path first
echo $HOME
If you are using bash or zsh you might have something like this
In ~/.bash_profile or ~/.bashrc or ~/.zshrc at the bottom of your file
export PYTHON_UTILS="$HOME/code/python/utils"
export PATH="$PYTHON_UTILS:$PATH"
Consider removing the .py from your file bc it is not needed in this case
Close and open your terminal, which is sourcing your file by its path
And now you should be able to treat your python file similar to a bash command
You don't need to use python3 filename.py to run the file, you can just use filename
From anywhere on your filesystem!
change alarm.py to include:
#!/bin/python
as the very first line in the file.
(or /usr/bin/python, depending on where you python interpreter is located. You can figure this out by typing: which python in the terminal.)
You can then just run alarm.py instead of python alarm.py.
e.g.:
~ toby$ alarm.py
And phihag who beat me by a few seconds is right, you need to add execute permissions (via chmod) to alarm.py.
You need to modify the Python specific path variable: PYTHONPATH.
So:
export PYTHONPATH=/Users/tobylieven/Documents/my_scripts
should get you working.
See: Python Module Search path
Something of interest that I really struggled with on OS X coming from Window, is that you its very hard to get the directory of your current script.
I found this.
#! /bin/zsh
cd "${0:h}"
Now you could execute a python file relative to the executed script instead of having to know the exact path where your python file is.
This might or might not help but I use this a lot to make my scripts and .command files work better.