How to set PYTHONPATH in Centos 7? - python

In Centos 7 system, what is the default pythonpath enviroment?
Before I make any pythonpath setting, the command "echo $PYTHONPATH" gives no output. After I make the following setting:
PYTHONPATH="{$PYTHONPATH}:/usr/lib/python2.7/site-packages:/usr/lib64/python2.7/site-packages/pandas:/app/anaconda2/pkgs"
export PYTHONPATH=$PYTHONPATH:/app/Jade
the command "echo $PYTHONPATH" gives the following output:
:/app/Jade
I don't understand why before "/app/Jade" there is an extra colon (:). And what is the correct way to set PYTHONPATH?
Best regards.
Yeping Sun

This has nothing to do with $PYTHONPATH but is a more generally PATH naming scheme. PATH is a colon-seperated list.
From What is path?
Thus, for example, to add a directory named /usr/test to a user's PATH variable, it should be appended with a text editor to the line that begins with PATH so that the line reads something like PATH=$PATH:$HOME/bin:/usr/test. It is important that each absolute path be directly (i.e., with no intervening spaces) preceded by a colon.
See more here: Python - PYTHONPATH in linux

export PYTHONPATH=/usr/lib/python2.7/site-packages:/usr/lib64/python2.7/site-packages/pandas:/app/anaconda2/pkgs:/app/Jade
The problem is in your first one, you included "" around $PYTHONPATH.
Secondly, the correct way to do this is:
export PATH=$PATH:/path/to/python
You can do which python to figure out what your path to Python is.
And then simply to export PYTHONPATH=/app/Jade <-- this may be incorrect as well since you need to supply this with an absolute path. Unless app is in your root folder, this won't work.
ALSO if you could copy-paste the exact error you are getting, that would be really helpful to the SO community in helping you, with this post and future posts.

Related

How to undo sys.path.append(pathToModule)

I have been trying to fix the python path on my cpu, and I originally was just trying to change my .bash_profile, but that wasn't working, so I used
import sys
sys.pat.append(path/To/Module)
and now when I run my script, I get this error message
How can I either fix this issue or undo the sys.path.append(path/To/Module)?
Also, is it possible to export multiple directories in the python path, and if so how do I do that?
Note that if you add a path with sys.path.append() you do this only for the current session. No need to undo it.
Just remove the line from you python file.
Have you tried sys.path.pop()
That will remove the last item that you added or indeed the last item on the PYTHONPATH, whatever that might be.
How can I either fix this issue or undo the >sys.path.append(path/To/Module)?
To undo the sys.path.append, you just need to remove that line from your script. Since the path is only modified for your current script and not system wide until you edit the PYTHONPATH.
Also, is it possible to export multiple directories in the python path, and if so how do I do that?
If you want to do it using sys, you can do it like:
sys.path.extend(['/path1', '/path2'])
recently I found that pycharm ide will automatically append certain directory to your sys.path if you marked that directory as Sources Root. So even if you use sys.path.pop(sys.path.index(**directory path**), it wouldn't work. Just unmark the folder and the issue will be solved.

Why would I add python to PATH

I am beginning to look at python, so when I found a tutorial it said that the first thing to do would be to download python from www.python.org/downloads/
Now when I downloaded python 3, I then started the installation and got to
Why would I want to "Add Python 3.5 to PATH"? What is PATH? Why is it not ticked by default?
PATH is an environment variable in Windows. It basically tells the commandline what folders to look in when attempting to find a file. If you didn't add Python to PATH then you would call it from the commandline like this:
C:/Python27/Python some_python_script.py
Whereas if you add it to PATH, you can do this:
python some_python_script.py
Which is shorter and neater. It works because the command line will look through all the PATH folders for python and find it in the folder that the Python installer has added there.
The reason it's unticked by default is partly because if you're installing multiple versions of Python, you probably want to be able to control which one your commandline will open by default, which is harder to do if both versions are being added to your PATH.
In addition to what #SuperBiasedMan stated, you can edit your PATH in Windows by hitting Start > Run, then type sysdm.cpl.
From there, navigate to Advanced tab and then hit Environment Variables.
In the lower section, where it says 'System variables', find the one named PATH and double click it. Note that it would be easier to copy and paste it to a notepad or something. The separator as you can see is a semi-colon.
Any path that you add to this variable, will be looked when you type any command in a cmd window or through the 'Run' command line.
That's the same concept as in Linux, I just pointed out how it can be edited.
This shows the way if you haven't add python to PATH(By the way, the python.exe is in my Python directory)
This is the way if you add python to a PATH

Python adding lots of things to PATH. How do I stop?

I'm finding that python is modifying my path in problematic ways -- in particular, it's pre-pending the path to my github development folder, which results in the wrong libraries being loaded.
In my terminal session, if I run echo $PATH I get:
~$echo $PATH
/Users/Nick/anaconda/bin:/usr/local/bin:/usr/bin:
/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin:/usr/texbin
Which is perfect. But when I launch python from that same terminal session (either as python or ipython) and check my PATH from within python, I'm getting:
>>> print(sys.path)
['', '/Users/Nick/anaconda/lib/python3.4/site-packages/Cython-0.22.1-py3.4-
macosx-10.5-x86_64.egg', '/Users/Nick/github/pandas',
'/Users/Nick/anaconda/lib/python34.zip', '/Users/Nick/anaconda/lib/python3.4',
'/Users/Nick/anaconda/lib/python3.4/plat-darwin',
'/Users/Nick/anaconda/lib/python3.4/lib-dynload',
'/Users/Nick/anaconda/lib/python3.4/site-packages',
'/Users/Nick/anaconda/lib/python3.4/site-packages/Sphinx-1.3.1-py3.4.egg',
'/Users/Nick/anaconda/lib/python3.4/site-packages/aeosa',
'/Users/Nick/anaconda/lib/python3.4/site-packages/setuptools-18.0.1-py3.4.egg']
Where are these coming from and how do I stop them?
Thanks!
PATH has nothing to do with the Python module search path; that environment variable is used by your shell to find executables, instead.
You need to look at the PYTHONPATH variable here.
If that variable doesn't contain your extra elements, start Python with the -S command line switch to disable loading site.py; it may be that the extra elements are set by a .pth file. Also see the site module documentation:
A path configuration file is a file whose name has the form name.pth and exists in one of the four directories mentioned above; its contents are additional items (one per line) to be added to sys.path.
While $PATH seems like it may be used by Python, what you actually want to look at is your $PYTHONPATH -- which is used by the import machinery and logic.
You should look into using virtualenvironments to control the pathing of Python module lookups.

Python default site-package directory changed after installing Ruby

I have spent a few hours trying to solve this problem but didn't manage yet. I figure my question is a bit specific to my own mac configuration and I need a more detailed instruction.
So my problem is that I installed Ruby using home-brew, for some reason, this installation changed my default python site-package directory. Before I have Ruby, my terminal python will automatically call the site-package from the path '/Users/xxxxxx/canopy/lib/python2.7/site-packages'. However, it now will use '/Library/Python/2.7/site-packages'. I tried three different ways to solve this issue but all failed:
1 use Canopy Terminal like this Set Canopy Preference, but this method point to another directory '/Applications/Canopy.app/appdata/canopy-1.5.2.2785.macosx-x86_64/Canopy.app/Contents/lib/python2.7/site-packages', which is not i want either.
2 change the bash_profile like this amend path, but by just putting the # in every line of the bash_profile, it still use directory '/Library/Python/2.7/site-packages'. Btw, my bash_profile and $PATH looks like this before:
and looks like this
3 add the path '/Users/xxxxxx/canopy/lib/python2.7/site-packages' into sys.path, which look like this now:
But none of those worked. How can I make my terminal point to the path '/Users/xxxxxx/canopy/lib/python2.7/site-packages' now? Or ultimately, what should I do to clean up those different site-packages and use the one I only need?
Thank you for any help!
On a mac by default the path is /Library/Python/2.7/site-packages/foo.egg.
Before doing anything, in terminal, start a new Python session and do
import sys
print sys.path
If the paths are of the form /Library/Python/2.7/site-packages/foo.egg, then just follow link
tl;dr
Basically add this to your bash_profile,
source [...] /activate
VIRTUAL_ENV_DISABLE_PROMPT=1 source [...] activate
I found the solution:
added to the bash_profile this line " export PATH="$HOME/canopy/bin:$PATH" ", and uncomment all other lines in bash_profile.

remove item from python path

I have added a path to the system pythonpath on linux and now i've broken it. How may I remove it ?
[EDIT]
Finally i solved it removing the script that added that path + installing something to rebuild the path.
not directly programming related but....
'print' your pythonpath to a file, edit the file and export that as your new pythonpath
[edit]
I stand corrected, see Banang answer below
[/edit]
I don't think there is a concept of Windows-like environment variables in Linux; they are defined in various scripts (e.g. .bashrc). You can edit those in any text editor.
What exactly did you to do "break" your PYTHONPATH?

Categories