I have recently installed kmos, a python package using pip in my user account on my institute cluster. How to create a module for kmos and set the path to the directory such that python accesses the library. Currently, I am giving the path to the kmos binary while running the program.
Linux distro: Cent OS
Module support: Lua-based Lmod environmental modules system
I am not exactly sure what you mean by "How to create a module for kmos". You didn't mention which terminal you are using. However it will be definitely helpful to understand the mechanism behind finding executables and python import.
If you want to execute the kmos command-line interface (e.g. kmos export ...) you need to make sure that wherever the kmos shell client is is in your $PATH variable. When you installed kmos (pip install --user --upgrade kmos) it should tell where it went. That directory needs to show up when you run echo $PATH. Most likely something like ~/.local/bin. If it doesn't show up you may want to put export PATH=${PATH}:~/.local/bin into your ~/.bashrc or the corresponding syntax in your echo $SHELL configuration file.
The other location is where the python module gets copied to. When you do the pip installation it should print this out as well. Most likely something like ~/.local/lib/pythonXY/site-packages. When you run python -c "import sys; print(sys.path)" it should include given directory. You can automatically add this directory again using your echo ${SHELL} configuration file like export PYTHONPATH=${PYTHONPATH}:~/.local/lib/pythonXY/site-packages.
If you can already import kmos from python then python -c "import kmos; print(kmos.__file__)" will tell you where it found it.
Related
I have an old computer with dozens of old python projects installed, each with its own different virtualenv, and many of which built with different versions of python.
I'd prefer not to have to download these different versions when I create new virtualenvs via virtualenv -p whatever path that version of python has
My question is: within a virtualenv, is there a command I can run to find the path to the version of python which was used to create that particular environment?
For example, if I created a venv with 'virtualenv -p /usr/bin/python3.4' and then ran this command with the venv activated, it would return '/usr/bin/python3.4'
Since virtualenv copies python completely (including the binary) there is no way to know the exact path it originated from.
However, you can easily find the version by running ./python --version inside the environment's bin folder.
You could try running something like this from the command line:
python -c "import sysconfig; print(sysconfig.get_config_var('BINDIR'))"
I installed python3.5.0 in /opt/python3.5.0/bin/, but I couldn't use it beyond this bin folder. I know this is a path issue. Could somebody point out the correct procedure to make python3 show up in the system for use? The operating system is CentOS6.6
The os needs to know where your python executable is.
Try running it from somewhere else
cd
/opt/python3.5.0/bin/python -V
If that works the your Python3 installation in opt is ok and you just need to let your shell (probably bash) know where the python binary is by changing your PATH environment variable.
export PATH=/opt/python3.5.0/bin/:$PATH
Run that line on the bash terminal and try running python again with no path like this:
python -V
If that works put the export PATH=/opt/python3.5.0/bin/:$PATH line in your .bashrc or .bash_profile so it will work in the console in future.
Failing all that take a look at setting the PYTHONHOME and PYTHONPATH environment variables.
Also a better approach might be to install python3 using your package manager. The python3 executable should give you a version of Python-3 if you have one installed. Though you might get a different version of Python3 than 3.5 that way.
I am trying to get a django project up and running, and I have django running, but I am having trouble with python's virtualenv.
here is my error (on terminal open this shows up)
/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: No module named virtualenvwrapper
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/Library/Frameworks/Python.framework/Versions/2.7/bin/python and that PATH is
set properly.
Here is what My .bash profile looks like:
# Setting PATH for Python 3.4
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.4/bin:${PATH}"
export PATH
# Setting PATH for Python 2.7
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
export WORKON_HOME=$HOME/.virtualenvs
source /Library/Frameworks/Python.framework/Versions/3.4/bin/virtualenvwrapper.sh
export PIP_VIRTUALENV_BASE=$WORKON_HOME
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
I am pretty new to command line stuff and virtual environments, so I might not know some "obvious" things I am doing wrong. Thanks.
First some basics of the command-line shell:
PATH is an environment variable that contains a list of filesystem directories. When you type a command such as ls, python or virtualenvwrapper.sh your shell will search each directory starting from the first one listed. To see your current PATH type:
$ echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
To see what a given command is going to resolve to the location of the program that is going to be run, use the which command:
$ which ls
/bin/ls
Now in your example you are first adding a Python 3.4 location to your PATH and then a Python 2.7 location. The latter location is going to be first on your PATH. So all your Python related commands are first going to try and run Python 2.7, if a command isn't found there, it next searches in your Python 3.4 installation.
You appear to have installed virtualenvwrapper for Python 3.4, but when you run virtualenvwrapper.sh from the line:
source /Library/Frameworks/Python.framework/Versions/3.4/bin/virtualenvwrapper.sh
You are explicitly running the virtualenvwrapper.sh installed for Python 3.4. This runs a python command where your Python 2.7 is run, which does not appear to have virtualenvwrapper installed, as shown by this error message:
/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: No module named virtualenvwrapper
If you tried removing the Python 2.7 location addition to your PATH and it didn't work, you have to remember that changes to your .bash_profile are not automatically reflected in your terminal. You have to create a new terminal session and that new session will read your new .bash_profile.
If you are trying to get the command virtualenv to resolve to two different Pythons, it's simply not possible. It will always resolve to one specific Python based on the ordering of the locations on your PATH. VirtualEnv does install alternate versions of the command with the version of the Python built into the name. Use the commands virtualenv-3.4 and virtualenv-2.7 to create virtual environments for each different Python.
The key to all of this is remembering that PATH is used to resolve to an absolute path location of a program this is run. Use the echo $PATH and which commands to help you understand how that final path is being resolved.
It may seem tedious, but typing out the full absolute path is always going to side-step the magic of PATH resolution:
$ /Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenv
Try using smaller steps on your way to manipulating the path. Use just virutalenv-3.4 to make a Python 3.4 virtual environment and only bring in virtualenvwrapper as you need it and after your understanding of shell environments is more solid.
Finally, even pro's can get tripped up by playing games manipulating the path. You are ultimately trying to take a bunch of different application locations and flatten them all into one namespace. At some point conflicts can become unresolveable. A Python installation tool such as Buildout has it's own learning curve, but it hard-codes the absolute path to the python of each python script that it installs. Hard-coding absolute paths is the only way to deal with extreme corner cases such as having two builds of Python 3.4 side-by-side where you have two scripts which each need to run on two different builds of the same Python. Hard-coding absolute locations is also desirable in production environments, because then your application is immune to any changes to the bash shell. If you forget about a required ordering of your PATH, or another sysadmin tinkers with the PATH, you won't find your application breaking unexpectedly.
Ok, I hate to start another of seemingly hundreds of other threads about this, but how do I set or tweak the PYTHONPATH? I'm very new to Mac and the only other time I've successfully done this was on Windows using Rapid Enviroment Editor. This looks like a whole different beast. I tried installing Django and it failed to do it for 2.7, while I need 3.4, so the current path must be for 2.7. Running which python in Terminal shows the /usr/bin/python directory, which I then can't cd into, let alone find by browsing. I see that my Python 3.4 directory has the Update Shell Profile file, but it has a lot of limitations. I also see other threads mention PYTHONPATH commands in IDLE and creating one of the bash profile type files for the Terminal. How can I set this and not worry about it anymore until I need to run a different version of Python? I'm on Mac 10.9.2.
"Explain like I'm five".
If you want to install packages for python 3.4 use: pip3 install django
When installing for python 2.7 just use: pip install django
To use python 3.4 type python3 in your shell.
To see where all installations of python are use: which -a python
Depending on how you installed the new versions of python you will see output like:
/usr/local/bin/python
/usr/bin/python
If you wanted to use the python in /usr/local/bin/python you can edit your .bashrc file and add export path="/usr/local/bin:$path".
Save, then type source .bashrc in your shell and when you type which python it will show something like /usr/local/bin/python
Don't screw around too much with different versions of python, you will end up causing yourself a lot of problems.
You should not have to change your PYTHONPATH, just specify which python or pip version you want to use and that will most likely be all you need to do.
To update PYTHONPATH you can run from the terminal:
export PYTHONPATH=$PYTHONPATH:/desired/path/to/add
Then to check the updated PYTHONPATH you can run:
echo $PYTHONPATH
I'm not sure if this completely answers your question, but this is one way to make sure modules are visible to python when you import them.
I just downloaded a python package and installed it on a Linux box using the following command:
python setup.py install --prefix=/home/ubuntu/dev/git/nx
That is, I did not use the default install directory. I then appended the existing PYTHONPATH variable in ~/.bashrc as follows:
export PYTHONPATH=/some/previous/path:/home/ubuntu/dev/git/nx
But when I run import of the package in python, I get a ImportError: No module named error.
Am I specifying the wrong path in PYTHONPATH? If so, how do I figure out which path to put there? Or is there a different error?
Thx
If you put it in bashrc you will either need to source ~/.bashrc or log in again. Depending on your distro, bashrc might not be the right place to put it. You're using Ubuntu, so it will work.
It's also generally good practice to include the current variable when exporting a path-type variable.
export PYTHONPATH=$PYTHONPATH:/home/ubuntu/dev/git/nx
You can verify that your path is correct with echo $PYTHONPATH. You can also run the above command to see if it will fix it (albeit temporarily and only in your current shell).
Ran
sudo python setup.py install
Didn't have to mess with PYTHONPATH. Working great!