I'm trying to add /home/garrett/bin/libpy/ to my python path so I can import my own libraries, I would also prefer not to overwrite the default python path but rather append my own path to the default. When I try to echo $PYTHONPATH or $PYTHONHOME I get nothing and even if I were to set it on the command line it wouldn't remain after the next reboot. I don't know where the config file for this is. Here's what the man pages had to say.

PYTHONHOME
              Change  the  location  of the standard Python libraries.  By default, the libraries are searched in ${prefix}/lib/python<version> and ${exec_prefix}/lib/python<version>, where ${prefix} and ${exec_prefix} are installa‐
              tion-dependent directories, both defaulting to /usr/local.  When $PYTHONHOME is set to a single directory, its value replaces both ${prefix} and ${exec_prefix}.  To specify different values for these,  set  $PYTHONHOME
              to ${prefix}:${exec_prefix}.

       PYTHONPATH
              Augments  the default search path for module files.  The format is the same as the shell's $PATH: one or more directory pathnames separated by colons.  Non-existent directories are silently ignored.  The default search
              path is installation dependent, but generally begins with ${prefix}/lib/python<version> (see PYTHONHOME above).  The default search path is always appended to $PYTHONPATH.  If a script argument is given, the  directory
              containing the script is inserted in the path in front of $PYTHONPATH.  The search path can be manipulated from within a Python program as the variable sys.path.

Dani AI

Generated

Summary and focused troubleshooting (builds on and posts)

The easiest, safest way to make your private library directory available without replacing Python’s defaults is to append that directory to PYTHONPATH in a shell startup file so it persists. Put the export line into the appropriate file for your session (interactive shells use ~/.bashrc, login/GUI sessions often read ~/.profile or ~/.bash_profile) and restart or source that file. Make sure the path uses a directory separator (slash) and not a typo that looks like a filename.

The import error you saw comes from using import syntax that includes a dot/extension. Python import expects module or package names, not filenames. A name with a dot is treated as a package qualifier, which is why Python complains the base item isn’t a package. On Linux also confirm exact filename and case — imports are case sensitive.

Practical checks and next steps

  • Verify the directory is actually on Python’s search path from an interactive Python session.
  • Attempt the import from that same REPL to see the same error in isolation; that narrows it to environment vs. code problems.
  • Confirm the module file is readable and named exactly as you import it.
  • If you want a package-style layout, add an init.py to the library directory and import using the package name.
  • For a longer-term, move the module into site-packages (or install it with pip --user / editable install) so you don’t have to manage PYTHONPATH manually.

Order of operations to resolve now: fix the shell startup entry (correct path, append not overwrite), open a new shell, check sys.path from Python, and import the module by its module name (no “.py”).

Recommended Answers

All 3 Replies

write

export PYTHONPATH="$HOME/bin/libpy:$PYTHONPATH"

at the end of the file /home/garrett/.bashrc

Then restart the terminal.

Since the current value of $PYTHONPATH seems to be null I devided that overwritting it wasn't such a bad thing, my .bashrc file currently has this line -> export PYTHONPATH="$HOME/bin.libpy" I have a very small program I'v written along with my imported library file but I'm still having problems.

Python program 'tags' in /home/garrett/bin/

#!/usr/bin/env python3

import sys
import MetaMP3.py

file = sys.argv[1]
tags = get_tags_mp3(file)

for i in tags:
    print(i)

Current librar file MetaMP3.py in /home/garrett/bin/libpy/

#!/usr/bin/env python3

import sys

def get_tags_mp3(song):
    f = open(song, 'rb')
    position = -95
    tag = b''
    counter = 0
    tags = []

    for i in range(0,2):
        f.seek(position, 2)
        line = f.read(30)
        position = -65

        while line[counter] is not 0:
            tag = tag + line[counter:counter + 1]
            counter = counter + 1

        counter = 0
        tag = tag.decode('utf-8')
        tags.append(tag)
        tag = b''

    return tags

Errors

garrett@mint-desktop ~/Downloads $ tags The\ Rains\ of\ Castamere.mp3 
Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 2195, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/garrett/bin/tags", line 4, in <module>
    import MetaMP3.py
ImportError: No module named 'MetaMP3.py'; 'MetaMP3' is not a package

import MetaMP3.py should be import MetaMP3.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.