I want to set pythonpath, but it doesnt work for other directory.
My bashrc:
export PYTHONPATH=/usr/lib/python2.7
export PYTHONPATH=$PYTHONPATH/plat-linux2:$PYTHONPATH/lib-dynload:$PYTHONPATH/dist-packages:$PYTHONPATH/lib-tk:$PYTHONPATH
If I keep only first line(single directory)
export PYTHONPATH=/usr/lib/python2.7
then, my bash shell takes me to the /usr/lib/python2.7 directory.
But when I include multiple directory -
export PYTHONPATH=$PYTHONPATH/plat-linux2:$PYTHONPATH/lib-dynload:$PYTHONPATH/dist-packages:$PYTHONPATH/lib-tk:$PYTHONPATH
It throws error like
bash: cd: /usr/lib/python2.7/plat-linux2:/usr/lib/python2.7/lib-dynload:/usr/lib/python2.7/dist-packages:/usr/lib/python2.7/lib-tk:/usr/lib/python2.7: No such file or directory
Don't use PYTHONPATH to construct more joined paths. Use some temp variable.
PY_BASE=/usr/lib/python2.7
PYTHONPATH=$PY_BASE:$PY_BASE/plat-linux2:$PY_BASE/lib-dynload
PYTHONPATH=$PYTHONPATH:$PY_BASE/dist-packages:$PY_BASE/lib-tk
export PYTHONPATH
Also, the cd command has nothing to do with the PYTHONPATH. Meaning you are doing something completely unrelated to try and cd into your PYTHONPATH.
Related
I am trying to add this python library to my MAC. Once the installation is done, I have to
Create environment variables:
NEUROD_ROOT = NeuroDecode path
NEUROD_DATA = path to the desired data folder
NEUROD_SCRIPTS = path to the desired scripts folder
I edited the file /etc/paths with nano:
sudo nano /etc/paths
And the file now looks like:
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
/Users/mathieu/Documents/NeuroDecode/scripts
NEUROD_ROOT=/Users/mathieu/Documents/NeuroDecode
NEUROD_DATA=/Users/mathieu/Documents/NeuroDecode/SubjectData
NEUROD_SCRIPTS=/Users/mathieu/Documents/NeuroDecode/SubjectScripts
Supposely, the file to start NeuroDecode is nd_gui.cmd. Not ideal for macOS I believe, and this file only includes start /B pythonw %NEUROD_ROOT%/neurodecode/gui/mainWindow.py. So I tried to shortcut it by adding an alias in my .zshrc file:
function runpy() {
python "$#"
}
alias neurodecode="runpy '/Users/mathieu/Documents/NeuroDecode/neurodecode/gui/mainWindow.py'"
Note: python is also alias and start python 3.8.5.
Sadly, it doesn't work. A KeyError is raised when the program tries to do os.environ['NEUROD_ROOT']. If I have a look at the elemnent os.environ._data, I have (incomplete screenshot):
Any idea what I did wrong and how I could properly set the path variables?
I have installed virtualenv and the virtualwrapper via apt-get, I got to a point where I created a virtual enviroment but however later on during that same day when I used the workon command it was not found. I further on went and inspected my home directory and .virtualenvs dir and the virtualenv I created earlier were still there.
Solving this problem took two steps:
Add this to your .bashrc / .bash_profile / .zshrc:
# load virtualenvwrapper for python (after custom PATHs)
venvwrap="virtualenvwrapper.sh"
/usr/bin/which -s $venvwrap
if [ $? -eq 0 ]; then
venvwrap=`/usr/bin/which $venvwrap`
source $venvwrap
fi
Then use:
source .bash_profile
# or .bashrc / .zshrc
to reflect the changes.
Additionally, if the terminal still sometimes cant find workon, use source .bash_profile to reset and find it again.
type source .profile in home directory from terminal.
Read the readme in the top of which virtualenvwrapper.sh
You need to source it inside bashrc
open ~/.profile
cd ~
nano .profile
add at the end
#virtualenvwrapper setup
export WORKON_HOME=$HOME/envs
export PROJECT_HOME=$HOME/dev
source /usr/local/bin/virtualenvwrapper.sh
to load your .profile file you just edited:
$ . .profile
I ran in to this problem too and I simply needed to logout and log back in.
This read in the changes which the debian package manager made to my system at /etc/bash_completion.d/virtualenvwrapper
I'm writing a python script which will be placed in a location. I want to execute it just like a command. for ex.
$ find_branch test
where find_branch is a script placed in anywhere in the system.
I would like to know how to achieve this. I can run it on the place where the script is present by chmod u+x on the script and removing the .py from the script
sudo nano /usr/bin/testpyscript
Then inside the script:
#!/usr/bin/python
print("I'm a python script")
Give it x permission:
sudo chmod +x /usr/bin/testpyscript
Now you can use it as a regular command:
bash-4.2$ testpyscript
I'm a python script
It doesn't have to be exactly at /usr/bin, any location that is inside your $PATH will do. Let's say you want it to be located at some folder inside your home directory, you could do something like this:
pwd
/home/brunorb
mkdir somedir
sudo mv /usr/bin/testpyscript somedir/
export PATH=$PATH:/home/brunorb/somedir/
testpyscript # from any folder in the system
I'm a python script
Make sure python has been added to your path and #!/usr/bin/python is located at the top of your script.
Note You could just try adding your script to your /usr/local/bin/ directory and give it the proper permissions.
sudo cp <your script> /usr/local/bin/
You have a number of options on how to achieve this.
Add the location where you put the script to your PATH environment variable, for example in your ~/.bashrc script:
export PATH="${PATH}:/folder/where/you/put/the/script"
Install the script to a location that is already on your path. It does not have to be a system folder like /usr/bin. Many default Bash setups will include ~/bin in your PATH.
Give the full path to your script on the command line:
/folder/where/you/put/the/script/find_branch test
Run the script through Python. This is very similar to option #2:
python /folder/where/you/put/the/script/find_branch test
Create an alias for the script in your environment. In bash you would do something like the following in your ~/.bashrc:
alias find_branch='/folder/where/you/put/the/script/find_branch'
OR
alias find_branch='python /folder/where/you/put/the/script/find_branch'
For options #1, #2, #3 and #5a to work properly, you should have a shebang with the version of python as the first line of the script. Any of the following will do, depending on how you have/want your environment set up:
#!/usr/bin/python
#!/usr/bin/python2
#!/usr/bin/python3
#!/usr/bin/env python
#!/usr/bin/env python2
#!/usr/bin/env python3
Finally, you do not have to remove the .py extension from the script if you do not want to. Many bash scripts have a .sh extension, for example, which does not prevent them from running as-is. You just have to include the extension in the name of the script when you run it.
Hello I am following this page.. I'm installing Python onto my mac so that I can set up a Django / Eclipse development environment. However I am not too sure how to go about executing this step:
The script will explain what changes it will make and prompt you
before the installation begins.
Once you’ve installed Homebrew,
insert the Homebrew directory at the top of your PATH environment variable.
You can do this by adding the following line at the bottom of your
~/.bashrc file
export PATH=/usr/local/bin:$PATH
Where do I find the bashrc file on my mac and where do I find the homebrew directory?
I am running a macbook pro with OS 10.8.5.
The .bashrc file is in your home directory.
So from command line do:
cd
ls -a
This will show all the hidden files in your home directory. "cd" will get you home and ls -a will "list all".
In general when you see ~/ the tilda slash refers to your home directory. So ~/.bashrc is your home directory with the .bashrc file.
And the standard path to homebrew is in /usr/local/ so if you:
cd /usr/local
ls | grep -i homebrew
you should see the homebrew directory (/usr/local/homebrew). Source
Yes sometimes you may have to create this file and the typical format of a .bashrc file is:
# .bashrc
# User specific aliases and functions
. .alias
alias ducks='du -cks * | sort -rn | head -15'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
PATH=$PATH:/home/username/bin:/usr/local/homebrew
export PATH
If you create your own .bashrc file make sure that the following line is in your ~/.bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
I would think you should add it to ~/.bash_profile instead of .bashrc, (creating .bash_profile if it doesn't exist.) Then you don't have to add the extra step of checking for ~/.bashrc in your .bash_profile
Are you comfortable working and editing in a terminal? Just in case, ~/ means your home directory, so if you open a new terminal window that is where you will be "located". And the dot at the front makes the file invisible to normal ls command, unless you put -a or specify the file name.
Check this answer for more detail.
On your Terminal:
Type cd ~/ to go to your home folder.
Type touch .bash_profile to create your new file.
Edit .bash_profile with your code editor (or you can just type
open -e .bash_profile to open it in TextEdit).
Type . .bash_profile to reload .bash_profile and update any
functions you add.
In my macOS Monterey version, zsh is the default terminal shell. zsh executes ~/.zshrc every time the terminal is opened.
vi ~/.zshrc
#Add your path export to .zshrc
PATH=/usr/local/bin:$PATH
Now, when you open the terminal, the path will be set correctly.
On some system, instead of the .bashrc file, you can edit your profils' specific by editing:
sudo nano /etc/profile
~/.bashrc is already a path to .bashrc.
If you do echo ~ you'll see that it's a path to your home directory.
Homebrew directory is /usr/local/bin. Homebrew is installed inside it and everything installed by homebrew will be installed there.
For example, if you do brew install python Homebrew will put Python binary in /usr/local/bin.
Finally, to add Homebrew directory to your path you can run echo "export PATH=/usr/local/lib:$PATH" >> ~/.bashrc. It will create .bashrc file if it doesn't exist and then append the needed line to the end.
You can check the result by running tail ~/.bashrc.
The .bash_profile for macOS is found in the $HOME directory. You can create the file if it does not exit. Sublime Text 3 can help.
If you follow the instruction from OS X Command Line - Sublime Text to launch ST3 with subl then you can just do this
$ subl ~/.bash_profile
An easier method is to use open
$ open ~/.bash_profile -a "Sublime Text"
Use Command + Shift + . in Finder to view hidden files in your home directory.
Open Terminal and execute commands given below.
cd /etc
subl bashrc
subl denotes Sublime editor. You can replace subl with vi to open bashrc file in default editor. This will workout only if you have bashrc file, created earlier.
When I want to run my python applications from commandline (under ubuntu) I have to be in the directory where is the source code app.py and run the application with command
python app.py
How can I make it (how is it conventionally done) to run the application from arbitrary directory with the command: app ? Similarly as you type ls, mkdir and other commands?
thank you
Add a shebang line at the beginning of your file:
#!/usr/bin/env python
Make your file executable by calling
chmod +x app.py
in the shell.
Move it to some location included in the PATH environment variable and rename it to app. Alternatively, add the path of the directory containing app to the PATH environment variable by adding the line
export PATH=$PATH:/path/to/app
to your .bash_profile.
Add the directory that the script is in to your path, make it executable, and add a proper shebang line.
In your .bashrc:
PATH=$PATH:/dir/to/the/script
Executable:
chmod +x myscript.py
At the top of the script, add the shebang line:
#!/usr/bin/env python
Then, from anywhere, you can just do:
myscript.py
(Note that you don't need a .py suffix, it could be called anything, e.g. app if you have a proper shebang line).
Add a shebang: as the top line of the file: #!/usr/bin/python or #!/usr/bin/python3 (you can use the python -B to prevent generation of .pyc files, which is why I don't use /usr/bin/env)
Make it executable: You will need to do chmod +x app.py
(optional) Add directory to path, so can call it anywhere: Add a directory with your executable to your $PATH environment variable. How you do so depends on your shell, but is either export PATH=$PATH:/home/you/some/path/to/myscripts (e.g. Linux distros which use bash) or setenv PATH $PATH:/home/you/some/path/to/myscripts (e.g. tcsh like in Mac OS X). You will want to put this, for example, in your .bashrc or whatever startup script you have, or else you will have to repeat this step every time you log in.
app.py will need to be in the myscripts (or whatever you name it) folder. You don't even need to call it app.py, but you can just rename it app.
If you wish to skip step #3, you can still do ./app to run it if you are in the same directory.
Probably you want to symlink to your file location instead of adding another location to the path
chmod +x app.py
ln ~app.py /opt/local/bin/app
...assuming that /opt/local/bin is already in your path,.
Also, do not forget to add the shebang line to the first line of your script:
#!/usr/bin/env python
A solution some what different from the ones mentioned here: Use an alias.
alias app='python /path/to/app.py'
Add the above line to your ~/.bashrc or ~/.bash_login file (or preferably to ~/.bash_aliases if you are on Ubuntu). Then you can simply use your script as a command line tool with app.
No need to add a shebang (thereby modifying your existing Python script), no need to make the script executable and no need to change your PATH.
I'm pretty sure you have to make the script executable via chmod +x and put it in the PATH variable of your system.