L.Kom
L.Kom

Reputation: 93

Import python file from terminal

I am trying to run a python file from my terminal. So when I run it I get the error below:

Traceback (most recent call last):
File "evaluate_classifier.py", line 1, in <module>
import alarm_detection.classifier_selection.classifier_evaluation as clf_eval
ImportError: No module named alarm_detection.classifier_selection.classifier_evaluation

In my file I use this code:

import alarm_detection.classifier_selection.classifier_evaluation as clf_eval

How can I import the python file classifier_evaluation from terminal (it is in a different directory)

Thanks.

Upvotes: 3

Views: 19018

Answers (3)

John Anderson
John Anderson

Reputation: 524

You have several options. They all have the same basic effect: You want to tell Python where to find the module your script needs to import.

Option 1. Alter the Python sys.path within your script.

(Limok pointed this out, too.) If you know the absolute path to the Python module, or you can calculate it from a relative path, then you can add that path in code before your script attempts to import the module.

import sys
sys.path.append('/path/containing/the/module/you/want/to/import')
# You can also manipulate sys.path in various ways:
sys.path.insert(0, '/path/to/search/before/other/locations')
sys.path.remove('/path/you/don't/want/to/search')

By fiddling with sys.path, you can achieve some pretty amazing (and confusing) effects. But be careful, because it can be confusing for other people who try to read and maintain your code later. ("Where did this module come from??")

Option 2. Alter the PYTHONPATH environment variable.

In the bash shell, this is as simple as

export PYTHONPATH=$PYTHONPATH:/path/containing/the/module/you/want/to/import
python yourscript.py

This has the same effect on sys.path, but it does not make your script responsible for configuring its own environment; it's a bit cleaner implementation, because you can run the same script on multiple machines without having to hack the script itself. Just copy the file to another machine and set the PYTHONPATH before you run the script.

This is pretty trivial in most shells. In Windows, you may need to use the GUI to set the Environment Variable PYTHONPATH.

Option 3. Use a .pth file.

Python looks at the PYTHONPATH variable to setup its sys.path list on startup. But it also looks for special .pth files in the Python site-packages folder. (If you don't know where the site-packages folder is, then that's probably another StackOverflow question.)

The simplest .pth file contains a single line with a file path:

/path/containing/the/module/you/want/to/import

Python will pick that up and add it to the sys.path list.

These .pth files can do a lot more, but that's beyond what you need right now. If you're curious, Bob Ippoli has more detail here: http://bob.ippoli.to/archives/2005/02/06/using-pth-files-for-python-development/

Option 4: Use a virtual environment.

This isn't directly a solution to your immediate problem, but I mention it for completeness. Python virtual environments allow you to contain all the dependencies for your application in a self-contained isolation chamber. All of the above (Options 1 through 3) still apply within a virtual environment, but sometimes a virtual environment helps to keep things tidy...and understandable.

If you can use virtualenvwrapper, it makes virtual environment management a breeze. Otherwise, you can use the low-level virtualenv (Python 2) and pyenv (Python 3) without much pain.

Upvotes: 0

Afiz
Afiz

Reputation: 493

is the package and python program that you are running are in same folder. If not, please place your package in the same directory and try to run the program again. Another way to add your package location to path of your system variables. if you are not using it regularly you can simply move package to your current folder where python program is present.

Upvotes: 0

Marses
Marses

Reputation: 1572

It seems the directory that the alarm_detection package is in, isn't in your python path. You can only import packages and modules from directories contained in your sys.path. The default directories contained in path are the current directory (where you ran the script from) and the main python directory where all your installed packages (like numpy, os, math etc...) are in.

You can (temporarily) modify the python path. If your package alarm_detection is contained in the directory /full/path/dir/ then you can import it from there like so:

import sys
# Adds the other directory to your python path.
sys.path.append("full/path/dir")

#Now this should work
import alarm_detection.classifier_selection.classifier_evaluation as clf_eval

You need to do this every time however, it doesn't stay added when you close that session of python.

Or, you can just move that package into the same directory you're using it in. I'm assuming in all this that alarm_detection is a package you created and wasn't downloaded by pip or anaconda or something.

Upvotes: 3

Related Questions