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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:28:53+00:00 2026-06-07T23:28:53+00:00

Suppose I have the following setup: mkdir test && cd test virtualenv .venv source

  • 0

Suppose I have the following setup:

mkdir test && cd test
virtualenv .venv
source .venv/bin/activate
pip install django
mkdir mod1
touch mod1/__init__.py
echo "a = 1" > mod1/mod2.py

Which gives me:

test/.venv
test/mod1/__init__.py
test/mod1/mod2.py

How would I write this function:

def get_module(module_name, root_path, virtualenv_path=None)

In order for this to work:

project_root_path = "./test"
project_virtualenv_path = "./test/.venv"

get_module("mod1.mod2", project_root_path, project_virtualenv_path)
get_module("django.contrib.auth", project_root_path, project_virtualenv_path)

Assuming I don’t have ./test/.venv activated.

The reason I want to do this, is because I’m working on a vim plugin which would implement gf functionality in a python file on an import statement. I’m trying to support virtualenvs as well.

EDIT:

Also, the script should not alter the current runtime, by adding or appending to sys.path. This should run inside vim, via the vim python bindings, and I don’t think altering the vim python runtime would be a good idea.

get_module could either return a module object, or the path to the module, which is what I’m basically looking for.

  • 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-07T23:28:55+00:00Added an answer on June 7, 2026 at 11:28 pm

    The only practical solution I could find here is to run the virtualenv’s activate_this.py script, look for what I need, then remove it’s changes from sys.path.

    import sys
    import os
    
    old_sys_path = list(sys.path)
    
    virtualenv_path = "/path/to/venv"
    activate_this_path = os.path.join(virtualenv_path, "bin", "activate_this.py")
    
    execfile(activate_this_path, dict(__file__=activate_this_path))
    
    # get my module here
    
    # restore sys.path
    sys.path = old_sys_path
    

    If you have a better answer, please add it, and I’ll change the accepted answer gladly.

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

Sidebar

Related Questions

suppose I have following test/ Main.py test/one/ One.py test1.txt test/two/ Two.py test2.txt What I
Suppose the following: I have a database set up on database.mywebsite.com , which resolves
Suppose I have following code package memoryleak; public class MemoryLeak { public static int
Possible Duplicate: Are file descriptors shared when fork()ing? Suppose I have following code in
Suppose I have the following simple struct: struct Vector3 { double x; double y;
Suppose I have the following js code: r='d|daa|dd'; reg = /\|.*/; result = reg.exec(r);
suppose I have the following matrix a = 2 NaN NaN 4 NaN 3
Suppose I have the following list: ID3_tag = ['Title','Artist','Album','Track'] And I have a file
Suppose I have the following directory structure in Vim's NERDTree: /some/folder/ |-apples.txt |-bananas.txt |-
Suppose I have the following method signature int f (int[] values) and I call

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.