I'm a long time windows user who has made the plunge to ubuntu. Some things I like, somethings I don't like and most...well most I just don't understand as of yet.

I mostly get Apt-get. Groovy. I mostly get the software center. Less groovy usually breaks, but whatever. One thing 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 have a python library I need to install. It's not one that you can get through apt-get or pip. FYI it's here: https://github.com/opendns/dnspython-clientsubnetoption

So I can download a zip file. Great. As far I can tell I have to then extract it to usr/share/doc (at least this is where I find the other libraries I have been able to install for python through apt-get). I have no idea how to do this though since the GUI doesn't want to let me do anything that's not in my account folder and 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 said library then?

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

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)

  • 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 ` – user1781837 Jan 23 '14 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. – user1781837 Jan 23 '14 at 9:18
  • Ahh, ok, not a package, right. Remove the '.' in front of '.clientsubnetoption'. I've edited the code sample – Sergey Jan 24 '14 at 0:34

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 ~".

  • 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. – user1781837 Jan 22 '14 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 Jan 23 '14 at 3:43
  • neither cd.. nor cd~ work on my ubuntu terminal. Both are "command not found." – user1781837 Jan 23 '14 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 Jan 24 '14 at 0:36

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.