简体   繁体   English

如何在 Linux 服务器中设置新用户以使用 Python?

[英]How to set up a new user in a linux server to work with Python?

I have added a new user in a linux server.我在 linux 服务器中添加了一个新用户。

While I can run simple python script, the new user get errors to import package that I can upload.虽然我可以运行简单的 python 脚本,但新用户在导入我可以上传的包时出错。

For instance例如

import matplotlib as plt

returns回报

no module named 'matplotlib'

That is because you probably did :那是因为你可能做了:

pip install --user matplotlib

to install matplotlib on your side.在你身边安装 matplotlib。

You have multiple solutions, here a non-exhaustive one (docker would be probably overkilled for that purpose imo) ordered from the worse to the best :您有多种解决方案,这里有一个非详尽的解决方案(为此目的,docker 可能会被过度使用),从最差到最好的顺序排列:

This can be for multiple reasons.这可能有多种原因。

matplotlib installed as user matplotlib 安装为用户

If you installed matplotlib with pip install --user package in a different user account.如果您在不同的用户帐户中使用pip install --user package安装了 matplotlib。 Don't expect another user to have that package available, it won't happen.不要指望其他用户可以使用该软件包,这不会发生。

Install the package with pip install package to be available system wide, for all users.使用pip install package以便在系统范围内对所有用户可用。

Note.笔记。 Depending on your specific setup you may need elevated permissions.根据您的特定设置,您可能需要提升权限。

matplotlib is installed in different python version matplotlib 安装在不同的 python 版本中

Irrespectively of how you installed the package (see above), you might have installed it in a different version than the one you're trying to run.无论您如何安装软件包(见上文),您可能已将其安装在与您尝试运行的版本不同的版本中。

To be sure you can import the package, as in python -m package , use python -m pip install package .为确保您可以导入包,如在python -m package ,请使用python -m pip install package This will ensure the same python interpreter is used to install as is used to execute.这将确保用于安装的 Python 解释器与用于执行的相同。

Better approaches更好的方法

one more standard approach一种更标准的方法

Learn and use virtual environments.学习和使用虚拟环境。 They are made to handle a python interpreter installation per project, rather than per system.它们用于处理每个项目而不是每个系统的 python 解释器安装。 Allowing you to keep dependent packages isolated from your system's python packages.允许您将依赖包与系统的 python 包隔离。

one slightly more advance approach一种稍微先进的方法

I'll use the opportunity to share a little Makefile that handles that crap for me.我将借此机会分享一个为我处理这些废话的小 Makefile。 Manages a virtual environment in the development folder, and keeps it up to date to with whatever packages are set in requirements.txt .管理 development 文件夹中的虚拟环境,并使其与在requirements.txt中设置的任何包保持同步。

Invoking make test is sufficient to run the test suite (or any other thing you may configure) in an environment with the correct packages.调用make test足以在具有正确包的环境中运行测试套件(或您可能配置的任何其他内容)。

.PHONY: test

# directory to store virtual environment
VENV_NAME=venv

# python runtime version
PYTHON_VER=3.7

# python executble
PYTHON=${VENV_NAME}/bin/python${PYTHON_VER}

# pip requirements file
REQUIREMENTS=requirements.txt

venv:           ## Recreates the virtual environment if needed.
venv: $(VENV_NAME)/bin/activate
$(VENV_NAME)/bin/activate: ${REQUIREMENTS}
    test -d $(VENV_NAME) || virtualenv -p python${PYTHON_VER} $(VENV_NAME)
    ${PYTHON} -m pip install -U pip
    ${PYTHON} -m pip install -r ${REQUIREMENTS}
    touch $@

test:           ## Runs the test suite.
test: venv
    $(PYTHON) -m pytest tests

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

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