Import python package from local directory into interpreter

Python

Python Problem Overview


I'm developing/testing a package in my local directory. I want to import it in the interpreter (v2.5), but sys.path does not include the current directory. Right now I type in sys.path.insert(0,'.'). Is there a better way?

Also,

from . import mypackage

fails with this error:

ValueError: Attempted relative import in non-package

Python Solutions


Solution 1 - Python

You can use relative imports only from in a module that was in turn imported as part of a package -- your script or interactive interpreter wasn't, so of course from . import (which means "import from the same package I got imported from") doesn't work. import mypackage will be fine once you ensure the parent directory of mypackage is in sys.path (how you managed to get your current directory away from sys.path I don't know -- do you have something strange in site.py, or...?)

To get your current directory back into sys.path there is in fact no better way than putting it there.

Solution 2 - Python

Keep it simple:

 try:
     from . import mymodule     # "myapp" case
 except:
     import mymodule            # "__main__" case

Solution 3 - Python

See the documentation for sys.path:

http://docs.python.org/library/sys.html#sys.path

To quote:

> If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first.

So, there's no need to monkey with sys.path if you're starting the python interpreter from the directory containing your module.

Also, to import your package, just do:

import mypackage

Since the directory containing the package is already in sys.path, it should work fine.

Solution 4 - Python

If you want to run an unmodified python script so it imports libraries from a specific local directory you can set the PYTHONPATH environment variable - e.g. in bash:

export PYTHONPATH=/home/user/my_libs
python myscript.py

If you just want it to import from the current working directory use the . notation:

export PYTHONPATH=.
python myscript.py

Solution 5 - Python

Inside a package if there is setup.py, then better to install it

pip install -e .

Solution 6 - Python

A simple way to make it work is to run your script from the parent directory using python's -m flag, e.g. python -m packagename.scriptname. Obviously in this situation you need an __init__.py file to turn your directory into a package.

Solution 7 - Python

A bit late to the party, but this is what worked for me:

>>> import sys
>>> sys.path.insert(0, '')

Apparently, if there is an empty string, Python knows that it should look in the current directory. I did not have the empty string in sys.path, which caused this error.

Solution 8 - Python

Using sys.path should include current directory already.

Try:

import .

or:

from . import sth

however it may be not a good practice, so why not just use:

import mypackage

Solution 9 - Python

I used pathlib to add my module directory to my system path as I wanted to avoid installing the module as a package and @maninthecomputer response didn't work for me

import sys
from pathlib import Path

cwd = str(Path(__file__).parent)
sys.path.insert(0, cwd)
from my_module import my_function

Solution 10 - Python

Speaking for python3.. I wanted to use an improved version of a library that's installed in my environment. There are some extra print statements it makes to show that it and not the original lib is being used.

I placed the lib's folder next to the python script. Ran the script.. it ran with the local lib with the modifications.

Removed the folder and ran it again - this time it ran with the installed lib.

So, solution was simple : place the lib's folder (with same name as in your import statement) in your project folder. That does the job, at least at my end.

This is on a standard Linux Mint 20.04 system, with a python 3.8 virutal environment activated (so "(py3.8)" appears in my terminal when I'm in the virtual env)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionprojectshaveView Question on Stackoverflow
Solution 1 - PythonAlex MartelliView Answer on Stackoverflow
Solution 2 - PythonGuido U. DraheimView Answer on Stackoverflow
Solution 3 - PythonSpoonMeiserView Answer on Stackoverflow
Solution 4 - PythonPierzView Answer on Stackoverflow
Solution 5 - PythonNagashayanView Answer on Stackoverflow
Solution 6 - Pythonbradley.ayersView Answer on Stackoverflow
Solution 7 - PythonmaninthecomputerView Answer on Stackoverflow
Solution 8 - PythonsunqiangView Answer on Stackoverflow
Solution 9 - PythonrdmolonyView Answer on Stackoverflow
Solution 10 - PythonNikhil VJView Answer on Stackoverflow