简体   繁体   English

Python - 在 Virtual Env 中安装包

[英]Python - Installing packages in Virtual Env

I am trying virtual env for the first time in python.我第一次在 python 中尝试虚拟环境。 I have Python 3.6 and Python 2.7 installed in my machine.我的机器上安装了 Python 3.6 和 Python 2.7。 I have installed all the necessary packages in Python 3.6.我已经在 Python 3.6 中安装了所有必要的包。 However, the code that I have seems to be compatible with Python 2.7.但是,我拥有的代码似乎与 Python 2.7 兼容。 Hence I opted for Virtaul Env.因此我选择了 Virtaul Env。 But I could not access the packages that i need.但是我无法访问我需要的包。 I have went through the questions in stack overflow but nothing helped.我已经解决了堆栈溢出中的问题,但没有任何帮助。 I have tried following steps:我尝试了以下步骤:

  • Created new project in pycharm在 pycharm 中创建新项目
  • used the interpreter as Python27使用解释器作为 Python27
  • in command prompt, I went to specific virtual env folder and tried pip install package_name在命令提示符下,我转到特定的虚拟 env 文件夹并尝试 pip install package_name
  • showing the error as below显示错误如下

    Fatal Python error: Py_Initialize: unable to load the file system codec File "D:\\Users\\username\\venv_name\\lib\\encodings\\__init__.py", line 123 raise CodecRegistryError,\\ ^ SyntaxError: invalid syntax Current thread 0x0000d85c (most recent call first):

I need either to install packages in the venv or transfer already installed packages into venv.我需要在 venv 中安装软件包或将已安装的软件包转移到 venv 中。

Thanks谢谢

I don't know how to solve your problem, since I don't use pycharm myself, but I can tell you what's causing it. 我不知道如何解决您的问题,因为我自己不使用pycharm,但是我可以告诉您是什么原因造成的。

It seems that you are trying to run Python 2 code under Python 3. The raise expression that fails for you is valid under Python 2, but not under Python 3. Therefore the SyntaxError . 似乎您正在尝试在Python 3下运行Python 2代码。对您失败的raise表达式在Python 2下有效,但在Python 3下无效。因此, SyntaxError So, you are probably using the source files from the virtualenv (see that ...\\venv_name\\... path?), but executing them in Python 3 interpreter. 因此,您可能正在使用来自virtualenv的源文件(请参阅...\\venv_name\\...路径?),但要在Python 3解释器中执行它们。

In order for you to install packages to your virtual environment you need to add it first in PyCharm and use it as your interpreter. 为了将软件包安装到虚拟环境中,您需要先在PyCharm中将其添加并用作解释器。

You can add your venv by: 您可以通过以下方式添加venv:

By going to Settings( Ctrl + Alt + s in windows) 通过转到设置(在Windows中为Ctrl + Alt + s

Click the icon next to project interpreter then choose add. 单击项目解释器旁边的图标,然后选择添加。 在此处输入图片说明

Browser for your virtual environment. 虚拟环境的浏览器。 在此处输入图片说明

After that you can now install packages to your virtual env by clicking the ➕ icon. 之后,您现在可以通过单击➕图标将软件包安装到虚拟环境中。 在此处输入图片说明


Also you cannot just install packages in your virtual environment by going to its directory, you need to activate it first by: 另外,您不能仅通过转到虚拟目录中的目录来将其安装在虚拟环境中,需要首先通过以下方式激活它:

Windows: c:\\path\\to\\your\\venv\\Scripts\\activate.bat Windows: c:\\path\\to\\your\\venv\\Scripts\\activate.bat

Linux/Mac: source path/to/your/venv/bin/activate Linux / Mac: source path/to/your/venv/bin/activate

From another SO post, the issue is PYTHONPATH.从另一个 SO 帖子中,问题是 PYTHONPATH。 Clear it, and you should be able to use 2 and 3.清除它,您应该可以使用2和3。

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

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