0

All my files are in a same directory

I'm fresh in python and I'm trying to code functions in a Preprocessing file like this:

#Preprocessing file
from dateutil import parser
def dropOutcomeSubtype(DataFrame):
    DataFrame.drop('OutcomeSubtype',axis=1,inplace='True')

def convertTimestampToTime(Serie):
    for i in range(0,len(Serie)):
        parser.parse(Serie[i]).time()

And then I'm trying to use it in a Exporting file like this:

#Import external librairies 
import pandas as pd
import numpy as np
import re

#import our librairy
from Preprocessing import convertTimestampToTime, dropOutcomeSubtype

#Reading
Datas = pd.read_csv("../Csv/train.csv", sep=",", na_values=['NaN'])

dropOutcomeSubtype(Datas)
convertTimestampToTime(Datas.DateTime)

And when i try to run the code in my OSX shell with this config: Python 3.5.2 |Anaconda 4.2.0 (x86_64)| IPython 5.1.0

I have get this error: cannot import name 'convertTimestampToTime'

and if change my import statement like this:

from Preprocessing import *

I get this error: name 'convertTimestampToTime' is not defined

Can you explain me why please ?

Thank you in advance

  • it searchs `Preprocessing.py` in "current working directory" - and it can be different than folder with script. If it can't find in CWD (current working directory) then it try to import installed modules - try `import Preprocessing` and `print(Preprocessing.__file__)` to see what file was imported. – furas Feb 04 '17 at 08:10
  • @furas And so ? I can't get your point. What do you advice me to do ? – Simon Laurent Feb 04 '17 at 08:12
  • first try `import Preprocessing` and `print(Preprocessing.__file__)` to see what file was imported. – furas Feb 04 '17 at 08:14
  • @furas it gives me this : /Users/SIMON/Desktop/Tx/Script/Preprocessing.py – Simon Laurent Feb 04 '17 at 09:52

1 Answers1

0

In this case you can add mod path to sys.path. if both in same dir add this code at first of main code

import os
import sys
here = os.path.abspath(os.path.dirname(__file__))
sys.path.append(here)
sahama
  • 669
  • 8
  • 16