简体   繁体   English

是否可以在不将模块安装到您的计算机上的情况下使用该模块?

[英]Is it possible to use a module without installing it on your computer?

For example, my college doesn't give students administrator privileges so I'm unable to install selenium webdriver.例如,我的大学没有给学生管理员权限,所以我无法安装 selenium webdriver。 The python code that I typed does not work.我输入的 python 代码不起作用。 Is it possible for me to use selenium without having it installed on the computer?我可以在没有安装在计算机上的情况下使用 selenium 吗? Are there any alternatives?有没有其他选择? Can I use a usb/thumb drive with selenium in it and have the code run it through that drive?我可以使用带有硒的 USB/拇指驱动器并让代码通过该驱动器运行它吗?

You're looking for sys.path which will do exactly what you want. 你正在寻找能完全符合你想要的sys.path

When you try to load a module, Python searches in the current directory for a module with that name. 当您尝试加载模块时,Python会在当前目录中搜索具有该名称的模块。 If it doesn't find something there, it searches other places in some order and if it isn't found anywhere it is allowed to look, it'll raise an ImportError . 如果它没有在那里找到某些东西,它会按某种顺序搜索其他地方,如果在任何地方都找不到它,那么它会引发一个ImportError By adding your path to the folder on your USB where you have a version of the module, it should work. 通过将您的路径添加到USB上您拥有该模块版本的文件夹,它应该可以正常工作。

import sys
sys.path.append("/path/to/your/folder")

import selenium

You can also print sys.path to see in which directories Python searches for modules. 您还可以打印sys.path以查看Python搜索模块的目录。

Yes it is possible and there are a couple ways to do it. 是的,这是可能的,有几种方法可以做到这一点。

One way would be to place it in the same directory as the script and import it. 一种方法是将它放在与脚本相同的目录中并导入它。

Another way would be to set your system path : 另一种方法是设置系统路径

import sys

script_path = "path/to/selenium"

if script_path in sys.path:
    print "oops, it's already in there."
else:
    sys.path.insert(0, script_path)

# proceed to import selenium here

Beside modification sys.path in a script, you can use PYTHONPATH environment variable. 除了在脚本中修改sys.path之外,您还可以使用PYTHONPATH环境变量。 According to The Module Search Path in documentation: 根据文档中的模块搜索路径

sys.path is initialized from these locations: sys.path从这些位置初始化:

  • the directory containing the input script (or the current directory). 包含输入脚本(或当前目录)的目录。
  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH ). PYTHONPATH (目录名列表,语法与shell变量PATH )。
  • the installation-dependent default. 依赖于安装的默认值。

On Windows for example: 在Windows上例如:

set PYTHONPATH=c:\path\to\modules;d:\another\path
python script.py

On Linux for example: 以Linux为例:

export PYTHONPATH=/path/to/modules:/another/path
./script.py

if you are on Windows environment then you may want to try: 如果您在Windows环境中,那么您可能想尝试:

  1. download the module from a standard repository for example [ https://pypi.python.org/pypi/urllib3] 从标准存储库下载模块,例如[ https://pypi.python.org/pypi/urllib3]
  2. unzip/untar the module 解压缩/解压缩模块
  3. run > python setup.py 运行> python setup.py

Depending on how the system is setup, the libraries should be installed into your user account. 根据系统的设置方式,应将库安装到您的用户帐户中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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