Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8882773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:39:41+00:00 2026-06-14T20:39:41+00:00

I have two modules: factors.py and primes.py. In factors.pyc, I have a function that

  • 0

I have two modules: “factors.py” and “primes.py”. In “factors.pyc”, I have a function that should find all the prime factors of a number. In it, I import 2 functions from “primes.py”. I have a dictionary in “primes.py”, which is declared as global (prior to it being defined). When I try to use it in the code of “factors.py”, I get this error:

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    pFactors(250)
  File "D:\my_stuff\Google Drive\Modules\factors.py", line 53, in pFactors
    for i in primes_dict:
NameError: global name 'primes_dict' is not defined

Here are my codes:

In “factors.py”:

def pFactors(n):
   import primes as p
   from math import sqrt
   from time import time
   pFact, primes, start, limit, check, num = [], [], time(), int(round(sqrt(n))), 2, n
   if p.isPrime(n):
      pFact = [1, n]
   else:
      p.prevPrimes(limit)
      for i in primes_dict:
         if primes_dict[i]:
            primes.append(i)
   #other code

And in “primes.py”:

def prevPrimes(n):
    if type(n) != int and type(n) != long:
        raise TypeError("Argument <n> accepts only <type 'int'> or <type 'long'>")
    if n < 2:
        raise ValueError("Argument <n> accepts only integers greater than 1")
    from time import time
    global primes_dict
    start, primes_dict, num = time(), {}, 0
    for i in range(2, n + 1):
        primes_dict[i] = True
    for i in primes_dict:
        if primes_dict[i]:
            num = 2
            while (num * i < n):
                primes_dict[num*i] = False
                num += 1
    end = time()
    print round((end - start), 4), ' seconds'
    return primes_dict #I added this in based off of an answer on another question, but it still was unable to solve my issue

prevPrimes(n) works in the manner it was intended to. However, because I am unable to access primes_dict, pFactors(n) doesn’t work.

How can I use the dictionary primes_dict (created in one module) in another module? Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:39:42+00:00Added an answer on June 14, 2026 at 8:39 pm

    Anything defined in primes will be under the name you import it as. Since you imported it as p, then primes_dict is accessible as p.primes_dict. You could, if you wanted, do

    from primes import primes_dict
    

    to have it as a top-level name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my code base I find that two modules have structures with the same
I have two modules A, B. A has a function f() that is globally
I have two modules that should over ride other urls, basically /management/category/edit/id/1 (Edit Category
I have two different modules that need access to a single file (One will
I have two distinct modules that can be used independently, but Module2 is dependent
Suppose we have two modules with cyclic dependencies: # a.py import b def f():
I have two modules like this (very very simplified): main.py: from window import *
Suppose I have two modules: a.py: import b print __name__, __file__ b.py: print __name__,
Assuming I have two Python modules and path_b is in the import path: #
I have two wifi modules M1 and M2 that connect to my access point.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.