3

When I run my Python 3 project, I always get error:

ImportError: No module named 'database'

I have an empty __init__.py file in the database folder. I am able to make it work if I run:

export PYTHONPATH="$PYTHONPATH:/youpath/myProject"

However, when I restart the system (Ubuntu), I get the same error again. How can I make it work in a permanent way?

1
  • Package up your project and install it. Your code will be installed to one of the existing places python looks for modules and packages. Python wheels is a good way to go.
    – tdelaney
    Commented Dec 18, 2015 at 16:22

2 Answers 2

1

The answer depends on what Shell you are using. If you are using bash add this to your ~/.bashrc

export PYTHONPATH="${PYTHONPATH}:/my/other/path"

I also tend to create "launch scripts" for many of my files as to not clutter my python path with too many paths as well as to gain a fine grain approach to what is available. Below is a sample run_script.sh file I use

export PYTHONPATH=$PYTHONPATH:<PATH VALUE 1>:<PATH VALUE 2>
echo $PYTHONPATH

python <path_to_script>
0

The way to use a package or module permanently is to install it. You can install directly from your source directory by writing a setup.py file and install it by python stup.py install (see distutils). Or you can go all-in and build your own installation bundles with wheels.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.