Products Resources Support About Us

Import function from file


#1

For several years, I was using the python 2.x version that was available from the IBM Tools & Toys page, but I really wanted to start using a 3.x version, so I downloaded 3.6.1 from your site. The install went fairly well, but I found that none of the programs I had coded would run any longer. I found a solution to this problem (right or wrong) that by coding

coding=latin_1

as the second line (#!/bin/env being the first) of the program, this seemed to fix my problem and have been using it since.

Recently, however, I started trying to put some commonly used functions in a separate source file, and calling them from other programs I had written, something like this:

from email_f import mail_it

When I run the main program, this is the result I get:
/u/sfr640/python> email_m.py
Traceback (most recent call last):
File “./email_m.py”, line 5, in
File “/u/sfr640/python/email_f.py”, line 1
{@\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd~\ufffd\ufffd\ufffd\ufffd\ufffdm\ufffd\ufffd\ufffd\ufffd@\ufffd\ufffd\ufffd\ufffdm\ufffd\ufffdM\ufffd\ufffd\ufffd\ufffdm\ufffd\ufffd\ufffd\ufffd\ufffd]z@@@@\ufffd\ufffd\ufffdm\ufffd\ufffd\ufffd\ufffd@~@}a\ufffd\ufffd\ufffda\ufffd\ufffd\ufffdm\ufffd\ufffd\ufffd\ufffd}@@@@\ufffd\ufffd\ufffd\ufffdm\ufffd\ufffd\ufffd@~@\ufffd}\u0199\ufffd\ufffdz@\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd|\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd}k@@@@@@@@@@@@}\ufffdz@\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd|\ufffd\ufffd\ufffdK\ufffd\ufffdK\ufffd\ufffd\ufffd\ufffd\ufffd}k@@@@@@@@@@@@}\u2902\ufffd\ufffd\ufffd\ufffdz@\u3162\ufffd@\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd}k@@@@@@@@@@@@}\ufffd\ufffd}\ufffd@@@@\ufffd\ufffd\ufffd@\ufffd\ufffd\ufffd\ufffd@\ufffd\ufffd@\ufffd\ufffd\ufffd\ufffdm\ufffd\ufffd\ufffd\ufffd\ufffdz@@@@@@@@\ufffd\ufffd\ufffd\ufffdm\ufffd\ufffd\ufffdK\ufffd\ufffd\ufffd\ufffd\ufffd\ufffdM\ufffd\ufffd\ufffd\ufffd]@@@@\ufffd@~@\ufffd\ufffd\ufffd\ufffdM\ufffd\ufffd\ufffdm\ufffd\ufffd\ufffd\ufffdk@}\ufffd}]@@@@\ufffd\ufffd\ufffd@\ufffd\ufffd\ufffd\ufffd@\ufffd\ufffd@\ufffd\ufffd\ufffd\ufffdm\ufffd\ufffd\ufffdz@@@@@@@@\ufffdK\ufffd\ufffd\ufffd\ufffd\ufffdM\ufffd\ufffd\ufffd\ufffd]@@@@\ufffdK\ufffd\ufffd\ufffd\ufffd\ufffdM]@@@@\ufffd\ufffd\ufffd\ufffdm\ufffd\ufffd\ufffd@~@}a\ufffd\ufffd\ufffda\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd@`\ufffd@L}@N@\ufffd\ufffd\ufffdm\ufffd\ufffd\ufffd\ufffd@@@@\ufffd\ufffd\ufffd\ufffd\ufffdM}\u3205@\ufffd\ufffd\ufffd\ufffd@\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd@\ufffd\ufffdz}k@\ufffd\ufffd\ufffd\ufffdm\ufffd\ufffd\ufffd]{@@@@\ufffd\ufffd@~@\ufffd\ufffdK\ufffd\ufffd\ufffd\ufffd\ufffdM\ufffd\ufffd\ufffd\ufffdm\ufffd\ufffd\ufffd]@@@@\ufffd\ufffd@~@\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffdK\ufffd\ufffd\ufffd\ufffdM\ufffd\ufffd\ufffd\ufffdm\ufffd\ufffd\ufffdk@\ufffd\ufffd\ufffd\ufffd\ufffd~\u3664\ufffd]@@@@\ufffd\ufffd\ufffd\ufffd\ufffdM}\u0645\ufffd\ufffd\ufffd\ufffd@\ufffd\ufffd\ufffd\ufffd@\ufffd\ufffd\ufffd\ufffd@\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd@\ufffd\ufffdz}k@\ufffd\ufffd]@@@@\ufffd\ufffd@~@\ufffd\ufffdK\ufffd\ufffd\ufffd\ufffd\ufffd\ufffdM\ufffd\ufffd\ufffdm\ufffd\ufffd\ufffd\ufffd]@@@@\ufffd\ufffd\ufffd\ufffd\ufffdM}\u0645\ufffd\ufffd\ufffd\ufffd@\ufffd\ufffd\ufffd\ufffd@\ufffd\ufffd\ufffd\ufffd@\u0105\ufffd\ufffd\ufffd\ufffd@\ufffd\ufffdz}k@\ufffd\ufffd]
^
SyntaxError: invalid syntax

To me, this seems like it might be another encoding issue, where the main program is not reading in the function module properly. I have verified that the function module will run fine as a stand alone program, but fails if it is imported by the other program.

Would you have any suggestions how I could get around this issue? Thanks.

John


#2

Make sure you have _BPXK_AUTOCVT=ON set in your environment, and be sure to tag all source files using chtag. In general, keeping source encoded as ASCII (ISO-8859-1, aka IBM 819) and properly tagged will help you avoid encoding problems.


#3

Thanks very much for your reply. I did have _BPXK_AUTOCVT=ON, so I recreated the file as ASCII and made sure it was tagged appropriately, and it now works perfectly. Thanks again.