I had the same problem as you i.e., I had some utility modules that may be useful to solve multiple project euler problems and each project euler problem had its own directory.
Initially I put the utility folder into sys.path
as @user3114046 suggested:
import sys; sys.path.insert(0, '../Primality Functions Folder')
from isPrime import isPrime
It felt dirty but it worked (note: I even used a relative path here!).
As an alternative, you could create a simple setup.py
file in the same directory as isPrime.py
:
from distutils.core import setup
NAME = 'isPrime'
setup(name=NAME, version='0.0.1', py_modules=[NAME])
and install your utility module:
$ python setup.py install
after that you could use it in any script:
from isPrime import isPrime
If you want to use several utility modules; you could put them into project_euler_utils
directory and install it as Python package. Put setup.py
along side project_euler_utils
directory:
from distutils.core import setup
NAME = 'project_euler_utils'
setup(name=NAME, version='0.0.1', packages=[NAME])
Run pip install project_euler_utils
, to install it. After that; you could use it from any script/module:
from project_euler_utils.isprime import isprime
Note: I've used lowercase names as pep-8 suggests (you need to rename your module and the function in it).
I tried using the fullest path I can think of with
"C:\Users\Owner\Documents\Primality Functions Folder"
, it gave me aSyntax Error: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape.
Use raw string literals for Windows paths r'C:\Users\Owner...'
(note: r''
prefix) Otherwise backslash is special inside literal strings, in particular u'\U0001F385'
is a not 10 characters; it is a single Unicode codepoint U+1F385
.