Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ Python plugin & Run classpath

Tags:

I have a project located at /home/myself/workspace/Project1, for which I created an SDK from a Python 2.7.3 Virtualenv I have setup.
This project uses some external code that I have in an accessible directory, e.g. /home/myself/LIBRARY; this directory contains several directories with code, docs etc....
For example, there is a module "important_util" located at /home/myself/LIBRARY/mymodule/important_util.py.

Now, I added the whole dir /home/myself/LIBRARY in the SDK Classpath, and in the Editor window it appears just fine. The imports and calls are recognized and I can also navigate through the code in LIBRARY directories.

The problem is that, when I try to run my program, it fails at the first import using LIBRARY!!!

Traceback (most recent call last):    File "/home/myself/workspace/Project1/my_program.py", line 10, in <module>       from mymodule import important_util as ut        ImportError: No module named mymodule 

I also tried to add the same directories to the section "Global Libraries" in the Sources section...but no luck.

I can't seem to find a way to add this code to the Run classpath, how would I be able to do this?

like image 424
mdm Avatar asked Dec 21 '12 17:12

mdm


People also ask

How do I get Python plugin for IntelliJ?

Press Ctrl+Alt+S , go to Plugins and inspect the Installed tab to ensure the plugin is enabled. Also make sure that the following prerequisites are met: Python SDK is downloaded and installed on your machine. The required framework SDKs are downloaded and installed on your machine.

Can I use IntelliJ for Python?

To develop Python scripts in IntelliJ IDEA, download and install Python and configure at least one Python SDK. A Python SDK can be specified as a Python interpreter for Python project. IntelliJ IDEA supports: Standard Python interpreters.

Is IntelliJ better than PyCharm?

PyCharm Professional Edition and the Python plugin for IntelliJ IDEA offer the same functionality. The main difference is that PyCharm Professional Edition is designed specifically for professional Python developers and provides a better UX for working with Python and its technologies.

How do I download IntelliJ plugins?

Press Ctrl+Alt+S to open the IDE settings and select Plugins. Find the plugin in the Marketplace and click Install.


2 Answers

Make sure you have __init__.py in mymodule directory:

The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later. ©

UPDATE: In IntelliJ IDEA additional directories should be added as Module Dependencies or configured as Libraries (to be added to the Dependencies) instead of the Classpath tab of the Python SDK:

Dependencies

I've verified that this folder (D:\dev\lib) is added to the PYTHONPATH and import works.

like image 111
CrazyCoder Avatar answered Sep 21 '22 16:09

CrazyCoder


In IntelliJ 14 it's a little different, you are modules/eggs like so:

  • Go to File -> Project Structure
  • Now select Modules and then "Dependencies" tab
  • Click the "+" icon and select "Library"
  • Click "New Library" and select Java (I know it's weird...)
  • Now choose multiple modules / egg and "OK".
  • Select "Classes" from categories.
  • Give your new library a name, "My Python not Java Library"
  • And finally click "Add Selected"
like image 29
Matthew Wilcoxson Avatar answered Sep 22 '22 16:09

Matthew Wilcoxson