2

I mostly understand apt-get and the Ubuntu Software Center, but one thing that I can't get at all is manually installing packages for which apt-get does not work. The file structure and layout of Ubuntu is really odd to me.

I am running Ubuntu 12.10 and have a Python library that I need to install. It's not one that you can get through apt-get or pip.

I have downloaded the zip file without any problem. As far I can tell I have to then extract it to usr/share/doc (at least this is where my other libraries store files to install for Python through apt-get).

I have no idea how to do this though since the GUI doesn't let me do anything that's not in my account folder. As far as I can tell, the usr is some completely disconnected file structure when it comes to a terminal (I can't simply back up a level from my user account to usr for instance).

What's the right way to install this Python library?

1
  • FYI, in case it matters, this is Ubuntu 12.10, sorry I don't know the alliterative animal name. Commented Jan 22, 2014 at 4:27

3 Answers 3

3

You should not put the file (or, actually, anything) into /usr... manually - this area is managed by Ubuntu's package manager.

I suppose you need the library because you want to write some Python script/program, right?

In this case you can just put it in the same folder as your program/script and do

from clientsubnetoption import ClientSubnetOption

Note that the library has a dependency on dnspython which, luckily, is in Ubuntu repositories so you can install it with

sudo apt-get install python-dnspython

(as a side-note: normally Python libraries are distributed as "python eggs" which are published in the central "store" called Python Package Index: https://pypi.python.org . From there they can be installed using special tools, such as easy_install, pip or zc.buildout. You can also create isolated Python environments using virtualenv and zc.buildout so there's absolutely no need to install even complex libraries system-wide)

3
  • Okay made sure the clientsubnetoption.py file is in the same folder as my script but when I run the script I get this: ` Traceback (most recent call last): File "test.py", line 1, in <module> from .clientsubnetoption import ClientSubnetOption ValueError: Attempted relative import in non-package ` Commented Jan 23, 2014 at 9:17
  • okay I've tried to get that mini-markup language to work a dozen times now and the message still looks like garbage. Commented Jan 23, 2014 at 9:18
  • Ahh, ok, not a package, right. Remove the '.' in front of '.clientsubnetoption'. I've edited the code sample
    – Sergey
    Commented Jan 24, 2014 at 0:34
0

Here's a suggestion, you need to start using an IDE if you're not already. Inside the link you provided is a single .py file, which you can save to ANYWHERE on your computer an then import it to an active Python project using an IDE.

Once it's in the project then you can call on it all you want.

I'm in the same boat as you, pretty new to Linux and started Python programming and personally I rarely use Terminal to do anything Python related.

As far navigating through terminal you go back a directory by typing "cd ..". You can jump straight to your Home directory by type "cd ~".

4
  • I do at times use an IDE (usualy eclipse), but I hate to be tied to one, if you see what I mean. For one thing in most programming competitions you can't use them, which granted in a programming competition you also aren't installing any libraries, but hopefully you get my drift. Commented Jan 22, 2014 at 19:55
  • 1
    """An IDE, or "Integrated Development Environment" will turn you stupid. They are the worst tools if you want to be a good programmer because they hide what's going on from you, and your job is to know what's going on. They are useful if you're trying to get something done and the platform is designed around a particular IDE, but for learning to code ... they are pointless.""" (from Learn C the Hard Way: c.learncodethehardway.org/book/ex0.html)
    – Sergey
    Commented Jan 23, 2014 at 3:43
  • neither cd.. nor cd~ work on my ubuntu terminal. Both are "command not found." Commented Jan 23, 2014 at 9:20
  • @user1781837: cd is a command, .. or ~ are parameters (parent directory and your home directory respectively). You need to put a space between the command and the parameters: cd ..
    – Sergey
    Commented Jan 24, 2014 at 0:36
0

A way you could use the script in any of your own python scripts is by adding it to the pythonpath. This is what the python interpreter uses to decide where to look for importable python packages. To add a directory or folder to the pythonpath type the following into the terminal:

export PYTHONPATH=$PYTHONPATH:/my/other/path

this will temporarily add "my/other/path" to the python path. "my/other/path" should be the path to the directory/folder that ClientSubnetOption is housed in. For example, you could put it in the directory ~/python_packages/clientsubnetoption. The ~ signifies the home directory.

If you would like to add this directory permanently to the pythonpath, add the above line to the bottom of your ".bashrc" file. It should be located somewhere in your home directory but it is a hidden file so if you are using the gui to find it you will need to press CTRL-h to view it.

The final thing you will need to do is make the directory ClientSubnetOption is housed in into a python package. This is actually much easier than it sounds. I would suggest putting ClientSubnetOption within its own directory with python_packages so the path would look something like ~/python_packages/clientsubnetoption/. then within the clientsubnetoption directory create a file called __init__.py. The file needs two underscores preceding and following init to work. This file doesn't need to contain any code it just needs to be present within the directory to make a python package.

Now that is done add the path to clientsubnetoption to your python path by opening the file .bashrc and adding the following line to the end of the file:

export PYTHONPATH=$PYTHONPATH:~/python_packages/clientsubnetoption/

Before any of these changes take effect, you will need to restart your shell by typing the following command into the terminal:

source ~/.bashrc

now you should be able to import clientsubnetoption by using the following import in your script:

from clientsubnetoption import ClientSubnetOption

hope that wasn't too convoluted.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .