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 6125163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:12:57+00:00 2026-05-23T16:12:57+00:00

I want to import some package depending on which value the user chooses. The

  • 0

I want to import some package depending on which value the user chooses.

The default is file1.py:

from files import file1

If user chooses file2, it should be :

from files import file2

In PHP, I can do this using variable variables:

$file_name = 'file1';
include($$file_name);
$file_name = 'file2';
include($$file_name);

How can I do this in Python?

  • 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-05-23T16:12:58+00:00Added an answer on May 23, 2026 at 4:12 pm

    Python doesn’t have a feature that’s directly equivalent to PHP’s “variable variables”. To get a “variable variable”‘s value (or the value of any other expression) you can use the eval function.

    foo = "Hello World"
    print eval("foo")
    

    However, this can’t be used in an import statement.

    It is possible to use the __import__ function to import using a variable.

    package = "os"
    name = "path"
    
    imported = getattr(__import__(package, fromlist=[name]), name)
    

    is equivalent to

    from os import path as imported
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i Want to import a given css or javascript file depending os some conditions,
I'm tring to create an SSIS package to import some dataset files, however given
I want to access some pages of web site https://myoffice.bt.com which requires user authentication
If I have some php classes inside a namespace com\test and want to import
I want to use some Python libraries to replace MATLAB. How could I import
I have a method I want to import from a DLL and it has
How do I import files in Python? I want to import: a file (e.g.
I have a MySQL file which I want to import via PHP 5. In
i want to add some options when a user presses the menu button, i
I created some files from a project in Unix, they are a lot and

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.