How do I properly setup pipenv in PyCharm?

2020-02-17 02:27发布

问题:

I need krakenex in a project, so I import it with

import krakenex

I have one version of krakenex in

/Users/x/Library/Python/3.6/lib/python/site-packages

. When I execute the script and

print(krakenex)

it shows me the path mentioned above.

In the future, I want to use the modules from the packages I installed with e.g.

pipenv install krakenex

with priority.

How do I manage to do that? Is it sufficient to add the path of the virtual environment to the sys path, or is there a more elegant way?

回答1:

You should be pointing your Project Interpreter to the virtualenv python bin. So in PyCharm File->Settings->Project: ProjectName->Project Interpreter, then a windows showing the Project Interpreter should be displayed.

Next to the top dropdown is a gear and your going to want to Add Local and navigate to the virtualenvs python bin. Something like virtualenvs/virtualenv_name/bin/python. Then your project should be pointing to the right place.



回答2:

To add more clarification on how to setup PyCharm with pipenv for now:

  1. Execute in your project directory

    pipenv --venv

Save the output, you'll reference this path later

  1. In PyCharm, Open Project Settings, and then select project interpreter

  2. Click Add a Python Interpreter > System Interpreter > Select Python Interpreter and paste the output from the first command, appending /bin/python onto the end.

Note that you will need to use the command line to install any packages since PyCharm currently doesn't support pipenv in their package management tools. However, I haven't had a problem with this method.



回答3:

PyCharm natively supports pipenv since version 2018.2. PyCharm 2018.2 will automatically create a pipenv when you open a project with a Pipfile, and makes it easy to create new projects with pipenvs.

For existing projects

As previously stated, for existing projects with a Pipfile, when you open a Python file, PyCharm will ask you if you want to install the dependencies from the Pipfile.lock.

For new projects

For a new project, you can use the project interpreter panel to create a Pipenv based project interpreter.



回答4:

Make sure to update PyCharm. I updated to 2018.3.

Export path for pipenv: $ export PATH="$PATH:Users/{user_name}/.local/bin"

PyCharm will then automatically detect pipenv under new environment using dropbox. Reference image here and see full blog post here.