Question

I am fairly new to Python (3.3.2) and I have minimal experience using

from .... import ....

beyond the use of time, turtle, and math. I am working on several Project Euler problems that require a function to determine the primality of a number using my simple isPrime(n) function. The isPrime(n) function is the only function in a file named isPrime.py.

I have a separate folder for each Project Euler problem I attempt to keep my code straight but I find it inconvenient having to copy my isPrime(n) function directly from isPrime.py and paste it into every new file within a folder devoted to each problem. I have some ideas about improving my isPrime(n) function and I don't want to have to open up several of the Problem folders just to change the lines in each file when I make some revisions.

So I thought that writing the following line at the top of each Problem's file would work:

from isPrime.py import isPrime or even from isPrime import isPrime

However, I encounter the following error both ways:

ImportError: No module named 'isPrime'

Here is a basic description of my folder hierarchy:

  1. ProjectEuler Folder
    • Primality Functions Folder
      • isPrime.py [contains the function isPrime(n)]
      • anotherFile.py
      • yetAnotherFile.py
    • Problem X Folder
      • problemX.py
      • something.py
      • somethingElse.py
    • Problem Y Folder
      • problemY.py
      • something2.py
      • somethingElse2.py
    • Problem Z Folder
      • problemZ.py
      • something3.py
      • somethingElse3.py

My question is:

What do I need to type in the opening lines of the files problemX.py,problemY.py, and problemZ.py to use the function isPrime(n) from the file isPrime.py in the folder Primality Functions?

I have searched at length here at stackoverflow and I see many questions are related to Python and difficulty with importing files/modules. However, I don't understand what a system path is, or what a relative import is, and I have no idea what the Python documentation found here is telling me. Please do not mark this question as duplicate of the many Python/Import questions already here. All I'm looking for is a simple 1-3 lines of code to place at the top of my files so I can import my functions from other folders and won't have to copy-and-paste my functions every time I attempt a new problem. I will be most appreciative of the most simply-worded answer. Thank you!

Was it helpful?

Solution

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 a Syntax 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.

OTHER TIPS

Since you want to keep the modules you are importing in different directories, you can tell python where to find them by adding those directories to sys.path:

from sys import path
path.append('fully specified path name of directory with module you want to import')
from mymodule import myfunction

This doesn't need to be done if isPrime.py is in the same folder as the program which imports it, but sometimes this is not practical.

It's not optimal for the module to have the same name as one of its functions, though, so I would consider changing it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top