1

steps taken

  1. navigated from the terminal to lib folder I created

  2. did pip install a_module_name -t . in the terminal

  3. went to file.py I am using module_name in, and put :

import os

import sys
# Fix path to library
sys.path.append(os.path.join(os.path.dirname(__file__), '/lib'))
import a_module_name

I'm getting an unresolved import error... How to fix?

My directory looks like:

/lib/a_module_name.py

file.py

1 Answers1

0

Though a virtual env is probably the right thing to do, I am going to answer your question assuming that you do not want to for some reason (e.g., you want to distribute this to someone who you do not expect to use virtualenv).

You should make lib a package by running touch lib/__init__.py. Then you can import directly from lib using import lib.a_module_name.

skyler
  • 1,487
  • 1
  • 10
  • 23
  • yeah... I thought there was some convention where lib is not supposed to be a package though.. for instance when I was using google app engine, I used this solution, http://stackoverflow.com/questions/34662595/google-app-engine-how-to-add-lib-folder/34668362#34668362 , which used google's vendor module, which messes with site-packages.. https://cloud.google.com/appengine/docs/python/refdocs/modules/google/appengine/ext/vendor#add –  Jan 17 '16 at 19:30
  • Are you familiar with `virtualenv`? If you are not, it is probably what you want because it does all of this for you. Right now I am assuming that you do know how to use it and have a reason not to. – skyler Jan 17 '16 at 19:34
  • Yeah, it just seems like virtualenv would add a lot of silly overhead for one only using just one third party module.. maybe not, I've used it only a couple times –  Jan 17 '16 at 19:41
  • I think it is worth using even just for one module. This is especially true if you are using version control (which you should be for just about anything). – skyler Jan 17 '16 at 19:43