What should I set my pythonpath to - python

I downloaded a python module from online and want to use it in IDLE, but I keep getting an error message saying it's not found.
My python path is "/Library/Frameworks/Python.framework/Versions/2.7/bin". If I want to be able to run modules I downloaded from online into IDLE, what should I set my python path to to be able to do that?
I am running OsX 10.10.5

Create a directory somewhere under your home directory. Let's say it's ~/pylib. Copy the module to ~/pylib. Before running IDLE execute the command
export PYTHONPATH=~/pylib
You should now be able to import the module.

If a module you have downloaded was properly packaged, it should come with the installation routine. Conventionally, it is called setup.py. If you see this file, you can just do
python setup.py install
That should take care of most packages available online and you don't even need to figure out where it was installed to.
If you really need to check/set the paths, you can check the directories as reported by
import sys
sys.prefix
sys.exec_prefix
These are the paths that point to the directories where stuff is installed (prefix/lib).
If you are looking into custom or user installation (using --user flag), check this folder: userbase/lib/python<VERSION>/site-packages
More details in the documentation.

Related

python cant find module in sys.path

I use the following command to install modules in home/.local since I dont have admin rights
pip install --install-option="--prefix=$HOME/.local" package_name
it is installed in my home dir and other cant run my script, so I move them to python lib/site-packages directory where others have access to. Then I add sys.path.insert(0, path_to_lib) in my script to import that module. This is how I install and use modules in my script and it always works fine. But when I did the same for a module "marrow.mailer", my script shows No module named marrow.mailer not sure why, the lib/site-packages folder does have that module. Please help me in finding solution for this. Thanks.

Installing a python package in a desired folder

I have downloaded a python package to install, on my ubuntu machine. The package has already a setup.py file to use, but I want to change the default python installation address to something else, for this package specifically (and not for good). So what I tried is:
First in the terminal, I export that address of the new folder:
export PYTHONPATH=${PYTHONPATH}:${HOME}/Documents/testfolder/lib/python2.7/site-packages
Then I add this exported address as prefix to the installation command:
python setup.py install --prefix=~/Documents/testfolder
The installation goes through. Now to make python always look for this new path as well (next to the default installation path), I export the address in bashrc file:
export PYTHONPATH="${PYTHONPATH}:~/Documents/testfolder/lib/python2.7/site-packages"
But now whenever I open a terminal and try to import the installed package, it cannot see ("no module named..."). Only when I open a terminal in the folder where I had the installation files (namely setup.py), and run python, can it then see the package, and it works there.
Why isn't my export in bashrc making the package available from anywhere?
Is there something I have done wrong in the above?
To answer your question about the export path. Do you have $PYTHONPATH as a part of your $PATH? If not you should add it to path.
The best way to handle this scenario in my opinion is to use a virtual python environment. There are a couple to choose from, but I like virtualenv the best. The reason to take this approach is because you can manage different versions of python in separate folders. And have separate packages installed in these folders. I recommend looking into it as it is a very useful tool. If you want an examole of how to use it i can provide that https://virtualenv.pypa.io/en/stable/

How to import python module

I'm working on a project for which I need to a module. I want to know how to import some other module in python that is not installed.
When I write
import xaut
It gives error no module named xaut.
I have xaut-0.4.5 that I downloaded from following link.
Please help me how to use it.
Well I think following will help
Extract your zip file
Open the command line (a linux terminal or cmd on windows). I am on linux so I have a terminal.
Now enter the directory that you have extracted.
In it is a directory "python" cd into it.
If you run the ls command (if on windows run dir) you will see that in this directory there is a script "setup.py". We need to execute it for installation.
Execute this script by giving command python setup.py install
This will hopefully install it and then you would be able to import it.
Basically you have only downloaded the package. To make a package work you also need to install it. So when you download a package always search for a setup.py file.
This is certainly a duplicate question. You can look up how to add a python module to the path in windows.
here is an example
How to add to the pythonpath in windows 7?

Python cannot find module even when path is appended to sys.path

Having some weird troubles installing python modules on my work computer (read: no admin/root rights), I'm using 2.7.5. I downloaded and unpacked the tarball and ran 'setup.py', but it had no effect: When I open the python shell, it can't find the module (this specific one is fuzzywuzzy). However, if I right click -> edit with IDLE the setup.py, and then run the shell from that file, it loads and works perfectly fine. Or, if I then open a new file from that shell, use the module and run it, it works fine. -__-
I've tried using:
import sys
sys.path.append('path here')
to append the location where the module is installed, but this doesn't help, nor does the path stay in the sys.path list when I close/reopen the shell.
This is actually driving me insane. Can anyone help? I'm relatively new to programming and python.
The best and easy way provided by python to install/uninstall packages is to use PIP.
use this
python -m pip install packagename==version
same way to uninstall
python -m pip uninstall packagename==version
if you are using windows you need to set path variable first usually python file will be in path C:\Python27 to set path variable
PATH=%PATH%;C:\Python27;

Hidden Markov Models (HMM) in Python

I am working with Hidden Markov Models in Python. For that I came across a package/module named hmmpytk. The problem is hmmpytk isnt pre-installed and when I download the hmmpytk module, i only get codes without the installation file. I use windows operating system. If I run a code simply with "from hmmpytk import hmm_faster" I get an Import error. ..so no idea how i get started with hmmpytk.
You need to make sure that the folder hmmpytk (and possibly also lame_tagger) is "in the directory containing the script that was used to invoke the Python interpreter." See the documentation about the Python path sys.path
EDIT: Alternatively, you can make sure that those folders are on your Python path. To see the folders in your path, type import sys; sys.path - the CWD is the first entry in the path if you started the interactive interpreter.
You probably want PIP or easy_install to install python packages.

Categories

Resources