Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm novice in this, and I have started learning Python, but I have some questions that I'm not be able to understand,
What exactly is the PYTHONPATH (on Ubuntu)? Is it a folder?
Is Python provided by default on Ubuntu, or does it have to be installed explicitly?
Where is the folder in which all modules are (I have a lot folders called python_)?
If I wish a new module to work when I'm programming (such as pyopengl) where should I go to introduce all the folders I've got in the folder downloaded?
Coming back from the PYTHONPATH issue, how do I configure the PYTHONPATH in order to start working on my new module?
PYTHONPATH is an environment variable which you can set to add additional directories where python will look for modules and packages. e.g.:
# make python look in the foo subdirectory of your home directory for
# modules and packages
export PYTHONPATH=${PYTHONPATH}:${HOME}/foo
Here I use the sh syntax. For other shells (e.g. csh,tcsh), the syntax would be slightly different. To make it permanent, set the variable in your shell's init file (usually ~/.bashrc).
Ubuntu comes with python already installed. There may be reasons for installing other (independent) python versions, but I've found that to be rarely necessary.
The folder where your modules live is dependent on PYTHONPATH and where the directories were set up when python was installed. For the most part, the installed stuff you shouldn't care about where it lives -- Python knows where it is and it can find the modules. Sort of like issuing the command ls -- where does ls live? /usr/bin? /bin? 99% of the time, you don't need to care -- Just use ls and be happy that it lives somewhere on your PATH so the shell can find it.
I'm not sure I understand the question. 3rd party modules usually come with install instructions. If you follow the instructions, python should be able to find the module and you shouldn't have to care about where it got installed.
Configure PYTHONPATH to include the directory where your module resides and python will be able to find your module.
PYTHONPATH is an environment variable
Yes (see https://unix.stackexchange.com/questions/24802/on-which-unix-distributions-is-python-installed-as-part-of-the-default-install)
/usr/lib/python2.7 on Ubuntu
you shouldn't install packages manually. Instead, use pip. When a package isn't in pip, it usually has a setuptools setup script which will install the package into the proper location (see point 3).
if you use pip or setuptools, then you don't need to set PYTHONPATH explicitly
If you look at the instructions for pyopengl, you'll see that they are consistent with points 4 and 5.
PYTHONPATH is an environment variable those content is added to the sys.path where Python looks for modules. You can set it to whatever you like.
However, do not mess with PYTHONPATH. More often than not, you are doing it wrong and it will only bring you trouble in the long run. For example, virtual environments could do strange things…
I would suggest you learned how to package a Python module properly, maybe using this easy setup. If you are especially lazy, you could use cookiecutter to do all the hard work for you.
Related
I'm new to Python, so I think my question is very fundamental and is asked a few times before but I cannot really find something (maybe because I do not really know how to search for that problem).
I installed a module in Python (reportlab). Now I wanted to modify a python script in that module but it seems that the python interpreter does not notice the updates in the script. Ironically the import is successful although Python actually should not find that package because I deleted it before. Does Python uses something like a Cache or any other storage for the modules? How can I edit modules and use those updated scripts?
From what you are saying, you downloaded a package and installed it using either a local pip or setup.py. When you do so, it copies all the files into your python package directory. So after an install, you can delete the source folder because python is not looking here.
If you want to be able to modify, edit, something and see changes, you have to install it in editable mode. Inside the main folder do:
python setup.py develop
or
pip install -e .
This will create a symbolic link to you python package repository. You will be able to modify sources.
Careful for the changes to be effective, you have to restart your python interpreter. You cannot just import again the module or whatever else.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 years ago.
Improve this question
I have installed Python 2.7.9 in /usr/local/bin. Now it doesn't work any more. I have another Python in /usr/bin/ but in the path is /usr/local/bin/ first. How can i remove the 2.7.9 Python?
Your question is lacking in details, the most pertinent being how you actually installed Python into /usr/local/bin. The installation method would indicate how to remove the installed files.
The most common way of installing packages into the /usr/local hierarchy of directories to is to compile from source and to run sudo make install after compiling and linking. If you didn't already remove the original (uncompressed) source directory, you can change into it and remove the compiled Python package by running:
sudo make uninstall
If the source code has been deleted, you could try re-downloading the source again.
If there’s no uninstall target for make (unfortunately, more common than you might think), another (inelegant) option is to use the find command to search for all files in /usr/local directory tree that have the same modification time as other files in the application that you want to remove.
These days, I would recommend installing the checkinstall tool. Instead of running make install, this can be used to create an RPM or Debian package which can then be installed (and uninstalled) using the system’s regular software installation tools.
DISCLAIMER: I've since learned a lot, and would recommend setting environment variables for a shell or shell session rather than use this answer. For example, if you manually relink the system's Python2 interpreter to a Python3 interpreter, you may wreak havoc on your system. Please use this answer with caution.
Just reset the symlink.
First, find out which python:
$ which python
In my case, I get:
/usr/local/bin/python
Then find where the symlink points to
$ file /usr/local/bin/python
/usr/local/bin/python: symbolic link to `/usr/bin/python'
Then just repoint the symlink back to the default (in this case, I use the default: /usr/bin/python).
No uninstalls necessary.
Update
I've since found a lot of better ways to enact this exact same behavior, without having effects on the entire system.
Say I have an undesired python install in /usr/bin, and a desired python install in /opt/bin. Let's say for the point of comparison that the /usr/bin is Python 3.5, and the /opt/bin is Python 2.7. This would create immediate consequences for using the wrong Python interpreter, rather than subtle errors down the line.
Application Defaults
If you would like to (on Linux systems) change which interpeter runs Python scripts, you can change this either via a GUI, or via xdg-mime (a walkthrough can be found here). For macOS or Windows, this can be done easily through a GUI.
Interactive Shell
If you would like to change the default Python for a specific shell, I can see two good ways of doing this. One would be to change the default search PATH to set /opt/bin before usr/bin for a specific situation, however, if you have numerous alternative installs to system packages, this might pose issues too. Another would be to set an alias for Python to the version you want to use. This is the preferred solution, as it only changes the interpreter and is merely a shortcut to reference an existing command.
For example, to set the alias I could use:
alias python="/opt/bin/python"
And to change the default path, I could use:
export PATH=/opt/bin:$PATH
Adding these lines to ~/.bashrc or ~/.bash_aliases (the latter is Ubuntu-only by default) will make these shortcuts be the default on any interactive shell you start. Combining application defaults and interactive shell scripting allows you to have tight control over which interpreter runs your code, but does not require interfering with potentially crucial system files.
Your PATH environment variable. It has a list of directories which bash searches (in the same order) when it's looking for an program to execute. Basically you want to put /usr/local/bin at the start of your PATH environment variable. Add the following to your ~/.bashrc file:
export PATH=/usr/local/bin:$PATH
You can have a look at the current setting by running the set command in bash.
Alternatively, you can simply rename /usr/bin/python to /usr/bin/python2.3 and create a symlink pointing to the new version, e.g.
ln -s /usr/local/bin/python /usr/bin/python
You can use checkinstall to remove Python, :
Install checkinstall
Use checkinstall to make a deb of your Python installation
Use dpkg -r to remove the deb.
See this post for more details.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
I am an amateur and have a mac and I would like to reset all python. I want to delete all the versions other than the one already on the mac in the OS. I would like the python versions to be as if I had just gotten a new OS.
THanks!!!
Unfortunately, there is no one-size-fits all answer here, because there are a number of different ways to install Python, many of which do not come with uninstallers.
Everything Apple installs is inside /System, or inside /usr but not /usr/local. So, those are the areas not to touch, no matter what.
Apple's Pythons put their system-wide site packages in /Library/Python/X.Y. Some third-party Pythons may also use site packages there. If you've mixed and matched, there's no way to straighten that up except to wipe the whole thing. To restore these directories to a clean slate, each one should have nothing but a site-packages directory, in which there should be nothing but a README and a easy-install.pth and/or Extras.pth.
Some third-party packages that have binary installers meant to work with Apple's Python install things into /usr/local/lib/pythonX.Y/site-packages. Again, these are shared with other Python installations. If you want to restore to a clean slate, delete everything in any such directory.
If you've configured user-specific site packages, or virtual environments, you should know which ones go with which Python—and, if you don't, just scrap them entirely.
Apple's Pythons install or link any scripts/executables that come with any site packages into /usr/local/bin. Unfortunately, most third-party Pythons will do the same thing. And of course non-Python executables installed from elsewhere also end up here. The only way to really be safe is to only delete files here that:
Are symlinks to something in /Library/Frameworks/Python.framework or into a site-packages directory.
Are scripts whose shebang lines points to a non-Apple Python (that is, it's not in /System or /usr/bin).
Are executables that link to a non-Apple Python (visible with otool -L).
If you're also trying to kill site packages installed into Apple Python, symlinks, shebangs, and executable links that point to Apple Python can go too.
Anything installed with a package manager—Homebrew, MacPorts, Fink, etc.—should be uninstalled the same way: brew uninstall python, sudo port uninstall python3.2, etc.
Anything that has an uninstaller (either inside a Python X.Y or MacPython or similar folder in Applications, or on the original disk image), obviously run that.
Meanwhile, non-Apple standard framework builds—that is, any binary installer from python.org, or anything you build yourself according to simple instructions—will put files into the following places:
/Library/Framework/Python.framework/X.Y. This is the main guts. Kill it. In fact, kill the whole Python.framework to remove all versions at once.
/usr/local/bin, shared with Apple, as mentioned above.
/usr/local/lib. Anything named libpython* here is killable.
/Applications/[Mac]Python*.
Non-framework builds by default install everything into /usr/local; they will be effectively the same as framework builds, but with no /Library/Framework/Python.framework/X.Y, and instead a /usr/local/lib/pythonX.Y that contains things besides just site-packages. Kill those things.
Third-party installations like Enthought or (x,y), you will have to figure out what you have and find the uninstallation instructions on their site. There's really no way around that.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Learning Python with the Natural Language Toolkit has been great fun, and they work fine on my local machine, though I had to install several packages in order to use it. Exactly how the NLTK resources are now integrated on my system remains a mystery to me, though it seems clear that the NLTK source code is not simply sitting someplace where the Python interpreter knows to find it.
I would like to use the Toolkit on my website, which is hosted by another company. Simply uploading the NLTK source code files to my server and telling scripts in the root directory to "import nltk" has not worked; I kind of doubted it would.
What, then, is the difference between whatever the NLTK install routine does and straightforward imports, and why should the toolkit be inaccessible to straightforward imports? Is there a way to use the NLTK source files without essentially altering my host's Python?
Many thanks for your thoughts and notes.
-G
Not only do you need NLTK on your PYTHONPATH (as #dhg points out), you need whatever dependencies it has; a quick local test indicates that this is really only PyYAML. You should just use pip to install packages. It's much less error-prone than trying to manually figure out all the dependencies and tweak the PYTHONPATH accordingly. If this is a shared host where you don't have the proper access to run a pip install, you should ask the host to do it for you.
To address the more general "Whatever the install script is doing" portion of your question: most Python packages are managed using setup.py, which is built on top of distutils (and sometimes setuputils). If this is something you're really interested in, check out The Hitchhiker’s Guide to Packaging.
You don't need system-install support, just the right modules where python can find them. I've set up the NLTK without system install rights with relatively little trouble--but I did have commandline access so I could see what I was doing.
To get this working, you should put together a local install on a computer you do control-- ideally one that never had NLTK installed, since you may have forgotten (or not know) what was configured for you. Once you figure out what you need, copy the bundle to the hosting computer. But at that point, check that you're using the module versions that are appropriate for the webserver's architecture. Numpy in particular has different 32/64 bit versions, IIRC.
It's also worth your while to figure out how to see the error messages from the hosting computer. If you can't see them by default, you could catch ImportError and display the message it contains, or you could redirect stderr... it depends on your configuration.
Let's assume that you have the NLTK source located in /some/dir/, so that
dhg /some/dir/$ ls nltk
...
app
book.py
ccg
chat
chunk
classify
...
You can either launch the python interpreter from the directory in which the nltk source directory is found:
dhg /some/dir/$ python
Python 2.7.1 (r271:86882M, Nov 30 2010, 10:35:34)
>>> import nltk
Or you can add its location to the PYTHONPATH environment variable, which makes NLTK available from anywhere:
dhg /whatever/$ export PYTHONPATH="$PYTHONPATH:/some/dir/"
dhg /whatever/$ python
Python 2.7.1 (r271:86882M, Nov 30 2010, 10:35:34)
>>> import nltk
Any other dependencies, including those that NLTK depends on, can also be added to the PYTHONPATH in the same way.
I want to be able to run my Python project from the command line. I am referencing other projects, so I need to be able run modules in other folders.
One method of making this work would be to modify the Pythonpath environment variable, but I think this is an abuse. Another hack would be to copy all the files I want into a single directory and then run Python. Is there a better method of doing this?
Note: I am actually programming in Eclipse, but I want to be able to run the program remotely.
Similar questions:
Referencing another project: This question is basically asking how to import
If you import sys, it contains a list of the directories in PYTHONPATH as sys.path
Adding directories to this list (sys.path.append("my/path")) allows you to import from those locations in the current module as normal without changing the global settings on your system.
Take a look at tools like
virtualenv, to set up a virtual python, in which you can install your modules without getting them globally. http://pypi.python.org/pypi/virtualenv
Setuptools, which allows you to specify (and automatically install) dependencies for your modules. http://pypi.python.org/pypi/setuptools (If you have problems with setuptools, take a look at Distribute, a maintained fork. http://pypi.python.org/pypi/distribute )
Buildout, which allows you deploy a complete application environment, including third-party software such as MySQL or anything else. http://pypi.python.org/pypi/zc.buildout/
First, I make sure that the module I want to include hasn't been installed globally. Then I add a symlink within the includee's directory:
# With pwd == module to which I want to add functionality.
ln -s /path/to/some_other_module_to_include .
and then I can do a standard import. This allows multiple versions etc. It does not require changing any global settings, and you don't need to change the program code if you work on different machines (just change the symlink).
If by "run modules" you mean importing them, you might be interested in this question I asked a while ago.
I just realised that I have actually solved this problem before. Here is the approach I used - much more complex than mavnn, but I was also solving the problem of running a Python2.x program from a Python 3.0
import os
import subprocess
env=os.environ.copy()
env['PYTHONPATH']=my_libraries
kwargs={"stdin":subprocess.PIPE, "env":env}
subprocess.Popen(["python","-u",program_path],**kwargs)