Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python / ImportError: Import by filename is not supported [duplicate]

Tags:

python

I'm trying to import a python file to my application which is written in python.

I have the following code:

import os
from os.path import basename

class specificClass:
    def dothing(self,path):
          runcommand = __import__("/root/"+ os.path.splitext(os.path.basename(path))[0]+ "/" + os.path.splitext(os.path.basename(path))[0] +"/sa/update.py")
          runcommand.main()

When I run it, it gives me the following error:

ImportError: Import by filename is not supported.
like image 658
Or Smith Avatar asked Sep 23 '14 14:09

Or Smith


1 Answers

Instead of doing a import like __import__ you can say

import sys
sys.path.append(path) # this is where your python file exists
import update
like image 114
hyades Avatar answered Oct 11 '22 17:10

hyades