Running python script from inside virtualenv bin is not working

后端 未结 5 2083
闹比i
闹比i 2020-12-24 06:11

I have a script I want to be available globally. I\'ve started it with the standard hashbang:

#! /usr/bin/env python

And linked it into the

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-24 06:47

    I think you are confused as to how virtualenv works.

    In a nutshell, virtualenv modifies your shell environment so that Python will look in different areas to find modules you want to import. There really isn't any relation between where you store your virtual environment and where you store your source files that you run in the virtualenv. If you wanted to, you could store your virtualenv in a directory called ~/environments/my_env, and all the source that you code while using your virtualenv in ~/projects/my_proj.

    You can read more about what virtulenv does in the docs.

    Really, the only thing that tells python where to find the modules are completely based on python (see the docs on how it works). Activating a virtualenv changes the way that python works.

    You can go back to having a shell script activate the virtualenv for you, or you can follow this recipe to activate it directly from your script.

    activate_this = '/path/to/env/bin/activate_this.py'
    execfile(activate_this, dict(__file__=activate_this))
    

    If you choose this route, keep the info the docs give in mind:

    This will change sys.path and even change sys.prefix, but also allow you to use an existing interpreter. Items in your environment will show up first on sys.path, before global items. However, global items will always be accessible (as if the --system-site-packages flag had been used in creating the environment, whether it was or not). Also, this cannot undo the activation of other environments, or modules that have been imported. You shouldn’t try to, for instance, activate an environment before a web request; you should activate one environment as early as possible, and not do it again in that process.

提交回复
热议问题