Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Can't Imort userlib #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
zakv opened this issue Jul 15, 2020 · 2 comments
Closed

Can't Imort userlib #63

zakv opened this issue Jul 15, 2020 · 2 comments

Comments

@zakv
Copy link
Contributor

zakv commented Jul 15, 2020

Hi All,

I'm attempting to get labscript running on a new computer with an anaconda developer install on Windows. The installation runs fine but I'm not able to import userlib, even after running labscript-profile-create. Instead I get ModuleNotFoundError: No module named 'userlib'. This occurs even with the default values in the labconfig. I'm able to import pythonlib just fine though.

In an interactive session I checked sys.path and it included C:\Users\UserName\labscript-suite\userlib and C:\Users\UserName\labscript-suite\userlib\pythonlib. I tried editing sys.path to move each of those up one directory, so sys.path then contained C:\Users\UserName\labscript-suite and C:\Users\UserName\labscript-suite\userlib. After that I was able to import both userlib and pythonlib in that interactive session without error.

Maybe the parent directories of userlib and pythonlib should be added to path instead?

def add_userlib_and_pythonlib():
"""Find the users's labconfig file, read the userlib and pythonlib keys, and add
those directories to the Python search path. This function intentionally
re-implements finding and reading the config file so as to not import
labscript_utils, since we dont' want to import something like labscript_utils every
time the interpreter starts up"""
labconfig = default_labconfig_path()
if labconfig is not None and labconfig.exists():
config = ConfigParser(defaults={'labscript_suite': LABSCRIPT_SUITE_PROFILE})
config.read(labconfig)
for option in ['userlib', 'pythonlib']:
try:
paths = config.get('DEFAULT', option).split(',')
except (NoSectionError, NoOptionError):
paths = []
for path in paths:
site.addsitedir(path)

Somewhat related: #43

This seems like an issue that others would have run into before so there may be something different about my set up somehow. Maybe it's because this computer never had a mercurial install of labscript on it? I checked sys.path on one of our other computers which still has a mercurial labscript install on it, and it included the paths to all three of C:\Users\UserName\labscript-suite, C:\Users\UserName\labscript-suite\userlib and C:\Users\UserName\labscript-suite\userlib\pythonlib,

Cheers,
Zak

@chrisjbillington
Copy link
Member

chrisjbillington commented Jul 15, 2020

Hi Zak,

Userlib itself isn't intended to be importable, rather its contents are. So for example a module foo as userlib/labscriptlib/foo.py is importable as import labscriptlib.foo

If you need a general-purpose dumping ground for Python modules, that's what pythonlib is intended for. A module bar as userlib/pythonlib/bar.py is importable as import bar.

(Since pythonlib is in userlib (perhaps a bad decision), bar may also be imported as import pythonlib.bar, but that's not advised)

As a side effect of the way the labscript suite installation used to work under mercurial, userlib was directly importable - this was because its parent directory (where the labscript suite modules were located) was added to the python path. That was not ideal (lots of random things in the labscript suite install directory were importable as a result), and is not the case anymore after the move to git and standard packaging techniques.

So I think this is working as intended! If you can confirm you're able to import your stuff given this explanation, then I'll close this issue. And let me know if I've misunderstood anything!

@zakv
Copy link
Contributor Author

zakv commented Jul 15, 2020

Ah I see, I've been writing all of my imports relative to userlib. I think I had somehow tripped the double import denier long ago and decided to always include userlib. in the import. Anyway just removing the leading userlib. from imports should be an easy fix, thanks for the help!

@zakv zakv closed this as completed Jul 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants