Skip to content

Insert user script directory at start of sys.path (not end) #958

@iangoldby

Description

@iangoldby

Windows 10, xlwings version 0.12.1

When I give my Python script a name that clashes with a module name in my Python environment, the RunPython() function loads the named module from the environment instead of my script. Normally Python looks for module imports in the current directory first, and then the system path.

To reproduce

  1. Set up a minimal Conda Python 3.6 environment and install xlwings. Note that even in this mininal environment the module test exists in the environment's lib directory.
  2. Create a new xlwings project called test: xlwings quickstart test --standalone
  3. Open test.xlsm, rename the _xlwings.conf sheet to xlwings.conf and set the Interpreter to point to the environment created in step 1 (e.g. C:\Users\Ian\AppData\Local\conda\conda\envs\temp\pythonw.exe)
  4. Execute the SampleCall() VBA macro.

Expected result: Script runs and writes Hello xlwings! to cell A1.
Actual result:
Traceback (most recent call last):

File "<string>", line 1, in <module>
AttributeError: module 'test' has no attribute 'hello_xlwings'

Workaround

Edit the VBA code in the xlwings module: Instead of sys.path.extend(...), which puts the user script directory at the end of the path, do sys.path[0:0] = ..., which puts the user script directory first in the path.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions