1

As per my reserach on the topic, when we import a Python module, Python searches for the module in following order:

  1. Built in Python modules
  2. sys.path entries (current working directory + PYTHONPATH environment variable)
  3. Some installation specific default folders (don't know that these are, possible site-packages etc?)

In this flow, can we manipulate this order somehow so that Python looks for a specific folder before checking current working directory? I'm specifically trying to achieve this in the context of a wsgi application.

asked Feb 28, 2020 at 12:42
6
  • This should only matter if you have two modules with the same name; and the solution could just be to rename one of them. Commented Feb 28, 2020 at 12:52
  • @kaya3 that is exactly my case, I have 2 versions of same package installed, and want to use one of them in a specific environment Commented Feb 28, 2020 at 12:55
  • @kaya3 here I explained the case in detail: stackoverflow.com/questions/60449533/… Commented Feb 28, 2020 at 12:56
  • I think if you're using pipenv or virtualenv for your project then the solution should be through that, rather than working around it. The whole purpose of a virtual environment is to be able to use different versions of Python and/or libraries for different projects, so I'd be very surprised if there's no way to set up a virtual environment with the right version of this module, using those tools. Commented Feb 28, 2020 at 13:03
  • @kaya3 a very particular case pushed me to search for a solution in this manner. The tool we use for deployment builds packages locally, and then deploy them. Here the problem happens if we build on a mac / windows and upload to lambda. So I need the same version of a module in both environments, but compiled in different environments. Commented Feb 28, 2020 at 13:09

1 Answer 1

1

Well, I'm not so sure about changing the order of importing because, in my opinion, the order depends on how the language itself is written but what you can do is this maybe-

sys.path = ['/path/to/application/app/folder'] + sys.path

which will allow you to add firstly the module that you created to the path array.

answered Feb 28, 2020 at 12:50
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.