简体   繁体   中英

No module named contextlib? in a virtualenv?

I have a virtualenv that is being used on multiple machines, through dropbox. Our dropboxes are all syncing to the same spot, (or at least symlinking so it appears thus) so, the relocation issues should be moot.

On my machine, everything works fine. I can create a virtualenv and it works fine. However, when someone else uses it, python gives No module named contextlib

When I create the virtualenv on the other person's computer, it appears to work on mine (I haven't tested exhaustively)

All computers are macs. Mine is running High Sierra, everyone else's is running Mojave, in case that matters.

How do I solve this problem? or at least, what is causing it?

I've seen Python: No module named contextlib? , but that seems to be an unrelated issue on embedded hardware.

NOTE: I understand that python thinks I shouldn't be doing this. However, I have a user base who are NOT programmers. They cannot manage their own venvs. They cannot handle complicated docker stuff. They want to type python foo.py and just have it work. In the past, I've gotten this to work with venvs.

If python can't handle this simple situation easily, then that's a problem, in my opinion.

OH, and I know that libraries are different for different OSes, I'm dealing with that more simply, by having a different set of venvs for the windows users. THAT works well, with no problems.

In general terms sharing the dependencies folder across computers and across different OS will give problems. This is something that now might work for your setup (all same OS same version), but as soon as you are going to be working with people that have windows or linux or even other versions of the same OS provider you will have problems.

Instead of looking for a workaround on how to fix the missing dependency for the contextlib , go to the root of the problem and fix the issue that you are generating by sharing the dependency folder. If people have issues with their environments, probably create a script to run the project, use a docker machine for provisioning or a virtual machine that will enforce always the same version as a base to run everything inside.

Here is a very good read about how to organise dependencies in a project (from the python docs)

Also, additionally I would suggest you to move the project to a docker container or similar, so that you can all be working under the same environment, and this on the long run will make easier to develop and deploy the code to the production server.

To run python on docker you can read this article , but there are many more, that could ease your work on the long run

If your users can't run pip install -r requirements.txt I assume they can't run either virtualenv --relocatable ENV right?

I have to say, you have to search new tools for your users. Maybe a script or some kind of docker image of your app with a simple run command might be easier.

From virtualenv documentation :

Normally environments are tied to a specific path. That means that you cannot move an environment around or copy it to another computer. You can fix up an environment to make it relocatable with the command:

 $ virtualenv --relocatable ENV 

There are important notes on the same page :

Note: you must run this after you've installed any packages into the environment. If you make an environment relocatable, then install a new package, you must run the command again.

Also, this does not make your packages cross-platform. You can move the directory around, but it can only be used on other similar computers.

It remains an experimental feature. Needless to say it's not a recommended production setup.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM