Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: ImportError: No module named 'database'

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 Answers

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>
like image 56
Brian Cain Avatar answered Jun 22 '26 22:06

Brian Cain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!