1

I have a script that functions from within ipython but when I try and run the same script from the command line I receive import errors for a local module that I am trying to import:

from helper_functions.email_from_server import send_email 

Error: ImportError: No module named helper_functions.email_from_server

This script imports from within Ipython without any issues.

Comparatively, I have code that runs without any issues within ipython I can run another script using the command: run script.py

From the command line I can run the same script: python /dir/script.py

However this python /dir/script.py doesn't work with the script with local imports (from above) and I can't figure out if its a pythonpath issue or some local env issue? I have been reading through stack to find it but haven't been able to thus far. It feels like its just around the corner

One attempted solution: PYTHONPATH=/dir/ python /dir/script.py

EDIT (to help clarify): I am using an anaconda distribution on a linux machine.

SteelyDanish
  • 629
  • 2
  • 8
  • 15
  • ipython is probably using some other python-executable. So maybe you installed anaconda and use ipython with that, but cmd calls some vanilla-python dist. But read your question again and check if you provided enough info. You did not even tell us what OS you are using. – sascha Mar 15 '17 at 00:40
  • Could you show us the script and the error message you get? Without knowing what you're seeing, it'll be very hard for anyone to help – Simon Fraser Mar 15 '17 at 00:40
  • @sascha -- thanks for the comments - edited to clarify – SteelyDanish Mar 15 '17 at 16:40
  • @SteelyDanish please read up on what makes a [mcve] - this isn't one. – Wayne Werner Mar 15 '17 at 16:41
  • @WayneWerner thanks - tried to refactor according to those standards – SteelyDanish Mar 15 '17 at 16:56

2 Answers2

1

Mucking about with PYTHONPATH is a recipe for sadness. You can do it, but you shouldn't. The correct thing to do is install your package in your correct environment. If you don't know how to create a package here's a super simple example. There may be some differences in your path when running via ipython vs command line.

You can find out what the differences are by using sys.executable and sys.path:

import sys
print(sys.executable)
print(sys.path)

Run that from IPython, and then run that from the python on your command line. You will undoubtedly get two different results. Since you're running Anaconda, you want to follow their guide for installing non-conda packages to install the one that you build.

Though of course that assumes that you've got the anaconda python on your path - you can check that out with which python since you're on Linux.

Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
  • Thanks Wayne - the sys.executable are the same for both the ipython and python. I don't have it built as a package - so maybe thats part of the issue. I was hoping to execute the scripts without having to build a package if possible. – SteelyDanish Mar 15 '17 at 18:33
  • If you're going to make a package you should make a package, otherwise you should just have a large file, separated into small functions and classes, and just run that. That assumes that you won't be reusing any of the code. If you're writing code for reuse, though, you're trying to create a package. I've added to my answer about the path - if `sys.path` is the same then something is pretty weird. – Wayne Werner Mar 16 '17 at 12:26
  • Thanks @Wayne Werner I checked the sys.path and they were pretty much identical with my python sys path having an extra path (location for anaconda/bin). If I put this line in --> /home/anaconda2/bin/python /project_folder/script.py will that import the sys path appropriately - that is what I am assuming. I tried to re-write the question. (http://stackoverflow.com/questions/42823059/crontab-issues-with-python-script) – SteelyDanish Mar 16 '17 at 15:21
  • This was super helpful in identifying if I had differences in my sys.path in ipython and python – SteelyDanish Mar 16 '17 at 17:56
0

I resolved it via creating a wrapper shell script. Ugly in that i'm exporting the python path each time, but it works.

#!/bin/bash
export PYTHONPATH="${PYTHONPATH}:/my/dir"
source ~/.bash_profile
cd /my/dir && my/anaconda/location/bin/python /my/dir/to/script/cript.py
SteelyDanish
  • 629
  • 2
  • 8
  • 15