How can I import external libraries in PyCharm?
Using a new virtual environment
I recommend that you create a virtual environment for your project. That will isolate your project's dependencies from your system-wide Python installation. PyCharm should propmt you to do so when you create a new project. Otherwise:
- Go to Settings > Project > Project Interpreter, click the cog button and select
Add...
- Select
New environment
. The default settings should be fine. Click OK. - Make a list of the packages you want to install in a file
requirements.txt
in your project's root directory, with one on each line, and then runpip install -r requirements.txt
from the PyCharm terminal
Existing interpreter
If you prefer to use an existing interpreter, though, you can also use it from PyCharm without recreating it. Any packages you have installed with that interpreter will ve available without reinstalling.
First, make sure that your Python interpreter in which you installed all your packages is available in PyCharm. Follow step 1. above, but replace step 2. with
- Select
Existing environment
. Click the three dots to locate your python interpreter in your file system. Click OK.
Then, once the interpreter is added, make sure to select it in Settings > Project > Project Interpreter, or use the interpreter menu in the bottom-right corner:
PyCharm include and modify External library in project
Well, you can add other directories as content roots:
Then simply mark the directory as a source root:
This should allow you to refactor, rename and do all the things you've wanted to do.
PyCharm and external libraries
I suggest to use Python virtual environment. It is really easy with PyCharm.
PyCharm > Preferences... > Project Interpreter > Python Interpreters
Click "Create Virtual Environment" and pick your base interpreter.
Click "Install" and install any packages you need. You can also add other repositories if the default ones do not contain required libraries. Another benefit is that you can see which libraries have newer version and can be updated.
Indexing of external library fails in PyCharm 4.5
Checked w/ JetBrains support, they confirmed that the only ways to add external libraries to a PyCharm project are:
- Add the library as a Content Root
- OR
- Open it as separate project in the same window and attach it to your current project
Not the cleanest approaches, as they both basically mean adding the other library's actual code to your project. But they are the only ones at this point.
Related Topics
Python - Using the Multiply Operator to Create Copies of Objects in Lists
When to Use Sys.Path.Append and When Modifying %Pythonpath% Is Enough
Is There a Difference Between Continue and Pass in a for Loop in Python
How to Let a Raw_Input Repeat Until I Want to Quit
How Is Tuple Implemented in Cpython
How to Use _Init_.Py to Define Global Variables
Anaconda/Conda - Install a Specific Package Version
Convert Pandas Datetimeindex to Unix Time
Tkinter Canvas Zoom + Move/Pan
How to Sort a Pandas Dataframe by Index
Safe Way to Parse User-Supplied Mathematical Formula in Python
What Do All the Distributions Available in Scipy.Stats Look Like
Login to Website Using Urllib2 - Python 2.7
How to Change the String Representation of a Python Class