0

I am trying virtual env for the first time in python. I have Python 3.6 and Python 2.7 installed in my machine. I have installed all the necessary packages in Python 3.6. However, the code that I have seems to be compatible with Python 2.7. Hence I opted for 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
  • used the interpreter as Python27
  • in command prompt, I went to specific virtual env folder and tried 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.

Thanks

DKS
  • 321
  • 1
  • 5
  • 9

3 Answers3

1

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.

You can add your venv by:

By going to Settings(Ctrl+Alt+s in windows or command+, in Macos)

Click the icon next to project interpreter then choose add. enter image description here

Browser for your virtual environment. enter image description here

After that you can now install packages to your virtual env by clicking the ➕ icon. enter image description here


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

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

Kelly Bang
  • 727
  • 6
  • 16
bgsuello
  • 582
  • 5
  • 12
  • When I attempt doing this, this warning me to close the python ("Close Program" error) – DKS Dec 26 '18 at 13:07
0

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.

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. So, you are probably using the source files from the virtualenv (see that ...\venv_name\... path?), but executing them in Python 3 interpreter.

  • cd c:\Python37 run python3.exe (i renamed file) I get the same error. why does running Python3 in its native directory parse py files in virtualenv directory that is NOT activated (the virtualenv stuff I have setup is all done for Python 2). – Gordon Oct 09 '19 at 09:20
0

From another SO post, the issue is PYTHONPATH. Clear it, and you should be able to use 2 and 3.

Gordon
  • 393
  • 1
  • 3
  • 8