Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Object-Oriented Python (retired) Objects Class Methods

Trouble importing Class to Interactive Window

I am using visual studio and PyCharm and I am having trouble importing a Class to the Interactive window. I type the following: "from monster import Monster" and I get an error message that tells me my variable is not defined.

I am thinking that it is a file path issue. Does anyone know the proper syntax to modify the "from monster import Monster" to account for the file path of monster?

Thanks for the help in advance.:

3 Answers

Julian Garcia
Julian Garcia
18,380 Points

rodian,

I uploaded and image of how is my project organized in pycharm : https://github.com/juliangarcia1/monster_prj/blob/master/capture.png

I created in windows, it is in github if you wish to compare: https://github.com/juliangarcia1/monster_prj

I hope this be helpful.

Julian Garcia
Julian Garcia
18,380 Points

In pycharm you need to specify the path of the python installation where all the libraries for that particular installation are contained.

in preferences > project > project Interpreter > select your project and specify in project interpreter 's scroll box the path to your python installation.

That is what I am using in pycharm in mac.

I found this link from BrainJets. https://www.jetbrains.com/help/pycharm/2016.1/configuring-python-interpreter-for-a-project.html

Before that you need to create a python project and all the files that your are importing must be in that project . So you need to included monster.py in the project and you can use from monster import Monster.

Hope this helps

Your help is greatly appreciated. I am still a tad confused. I don't see where to find preferences in reference to "in preferences > project > project Interpreter >"

I do notice a button labeled "monster" on the top right part of the screen in PyCharm that has a drop down where I can select "Edit Configuration" When I go into "Edit Configuration" I can see the working directory, the interpreter name and the script. I am just not sure what to do with this information.

Lastly, I see that the file monster.py appears to be in the project file. But having no experience with this, I'm still not sure it's in the correct location. The class is in monster and monster is in the project file.

Julian Garcia
Julian Garcia
18,380 Points

For the files in project, for example, I have a project named playground the structure in project`s pycharm window is:

playground
|--my_scripts
    |--__init__.py        # Empty file just to indicate it is a package and be available in test.py
    |--machines.py
|--test.py
|--animals.py

if I want to import a class Dog from animals.py in test.py

from animals import Dog

if we want to import class Engine from my_scripts/machines.py

from my_scripts.machines import Engine

Julian, thank you for the very clear explanation. But for whatever reason, it doesn't seem to be working right. If you don't mind, can you take a look at the directory tree below to see if I am making any obvious mistake.

I have C:\Users\Rodian\PycharmProject\My_First_Project

The project name is My_First_Project monster.py

So in the Interactive window I write: from My_First_Project.monster import Monster

I get an import error: No Module named 'My_First_Project'

Thanks for your help.