Python can't find module in the same folder
Your code is fine, I suspect your problem is how you are launching it.
You need to launch python from your '2014_07_13_test' directory.
Open up a command prompt and 'cd' into your '2014_07_13_test' directory.
For instance:
$ cd /path/to/2014_07_13_test
$ python test.py
If you cannot 'cd' into the directory like this you can add it to sys.path
In test.py:
import sys, os
sys.path.append('/path/to/2014_07_13_test')
Or set/edit the PYTHONPATH
And all should be well...
...well there is a slight mistake with your 'shebang' lines (the first line in both your files), there shouldn't be a space between the '#' and the '!'
There is a better shebang you should use.
Also you don't need the shebang line on every file... only the ones you intend to run from your shell as executable files.
Importing modules from parent folder
It seems that the problem is not related to the module being in a parent directory or anything like that.
You need to add the directory that contains ptdraft
to PYTHONPATH
You said that import nib
worked with you, that probably means that you added ptdraft
itself (not its parent) to PYTHONPATH.
Expand Python Search Path to Other Source
There are a few possible ways to do this:
- Set the environment variable
PYTHONPATH
to a colon-separated list of directories to search for imported modules. - In your program, use
sys.path.append('/path/to/search')
to add the names of directories you want Python to search for imported modules.sys.path
is just the list of directories Python searches every time it gets asked to import a module, and you can alter it as needed (although I wouldn't recommend removing any of the standard directories!). Any directories you put in the environment variablePYTHONPATH
will be inserted intosys.path
when Python starts up. - Use
site.addsitedir
to add a directory tosys.path
. The difference between this and just plain appending is that when you useaddsitedir
, it also looks for.pth
files within that directory and uses them to possibly add additional directories tosys.path
based on the contents of the files. See the documentation for more detail.
Which one of these you want to use depends on your situation. Remember that when you distribute your project to other users, they typically install it in such a manner that the Python code files will be automatically detected by Python's importer (i.e. packages are usually installed in the site-packages
directory), so if you mess with sys.path
in your code, that may be unnecessary and might even have adverse effects when that code runs on another computer. For development, I would venture a guess that setting PYTHONPATH
is usually the best way to go.
However, when you're using something that just runs on your own computer (or when you have nonstandard setups, e.g. sometimes in web app frameworks), it's not entirely uncommon to do something like
import sys
from os.path import dirname
sys.path.append(dirname(__file__))
Related Topics
Performing a Getattr() Style Lookup in a Django Template
Python: Maximum Recursion Depth Exceeded
What Is the Good Python3 Equivalent for Auto Tuple Unpacking in Lambda
How to Build a Recursive Function in Python
Stream Large Binary Files with Urllib2 to File
Handling Backreferences to Capturing Groups in Re.Sub Replacement Pattern
Dll Load Failed Error When Importing Cv2
Tkinter Adding Line Number to Text Widget
Reversing 'One-Hot' Encoding in Pandas
What Is the Cause of the Bad Request Error When Submitting Form in Flask Application
Subclassing Tuple with Multiple _Init_ Arguments
Module' Object Has No Attribute 'Loads' While Parsing JSON Using Python
How to Sort Python List of Strings of Numbers
How to Put Parameterized SQL Query into Variable and Then Execute in Python
Efficiently Convert Uneven List of Lists to Minimal Containing Array Padded with Nan