Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't import Python module located on root directory from a different path

Tags:

python

flask

Directory Structure:

    - / 
     - root
     - var
       - www
     - ...

In /var/www, when I try to import a python module, let's say Library, located in /root/anaconda3/lib/python3.5/site-packages, it gives me an Internal Server Error.

But, it works well when I run the same script in the root directory.

How can I make it work in /var/www? Any suggestions?

Edit:

import Library
...
like image 517
Kevin Avatar asked Apr 22 '26 00:04

Kevin


1 Answers

You need to add the path to your 'Library' to your Pathon search path. You can do this from within you Python script.

import sys
sys.path.append('/root/anaconda3/lib/python3.5/site-packages')

import Library

Also you can add the path to your PYTHONPATH environment variable.

like image 66
MrLeeh Avatar answered Apr 25 '26 19:04

MrLeeh