5

Python newbie here. Am using Anaconda (on Windows 7) by recommendation of a friend.

What I want to be able to do is make a change to my class in Notepad++ and then test it in my Python command prompt window immediately.

This is a simple question that I can't seem to find the answer: in what directory does a default installation of Anaconda expect me to be storing my .py files (so that I can easily load them using import <MODULE NAME>)?

My PATH variable is set to: C:\USERS\<USERNAME>\Anaconda3;C:\USERS\<USERNAME>\Anaconda3\Scripts

(this is the default)

Am I supposed to be working out of the Scripts directory...? There's already a lot of files in there.

What do most people do? Perhaps add another folder to the PATH variable and work out of there? Do you add a new folder to PATH for each project or is there a way that makes more sense? I already have a Projects directory I use for everything else. I'd like to just be able to work out of there.

I am coding in Notepad++. I don't really want to bother setting up/learning an IDE (I'm just doing relatively simple I/O file manipulation that I have previously been doing in Excel... the horror).

Sorry for the extremely newbie question. I searched and could not find anything relevant.

EDIT AFTER ACCEPTED ANSWER:

The problem was that I was running python.exe from the Start menu. I did not realize that you are supposed to open a CMD window in the folder (SHIFT+RIGHT CLICK) you are working in (such as C:\USERS\<USERNAME>\MY PYTHON STUFF) and run python from there.

Rick
  • 43,029
  • 15
  • 76
  • 119
  • 1
    I would recommend against this because it's a fairly advanced issue, but if you really want to do it... [link](https://docs.python.org/3/reference/import.html). Otherwise, just keep to working inside your folder. The "custom" modules I use just reside in a generic folder that handles my snippets and I copy them to my project folders if needed. Saves me the hassle of having to deal with the `import` system (which I don't really need to mess around with). – WGS Oct 21 '14 at 14:51
  • How do I "work inside my folder"? I am coding in Notepad++. I don't really want to bother setting up/learning an IDE (I'm just doing relatively simple I/O file manipulation that I have previously been doing in Excel... the horror). I want to make a change to my class in Notepad++ and then test it in my Python command prompt window. – Rick Oct 21 '14 at 15:06
  • You're skipping an awful lot of tutorials for a beginner. Just save the `.py` file *anywhere*, go to that directory from the terminal, then use `python .py`. Another option is to just double-click the file if it's supposed to run right away without any arguments. – WGS Oct 21 '14 at 15:08
  • Thanks, I already know how to do that but the problem is my file defines a class. It doesn't do anything. I want to create an object and use its methods to test it without writing testing procedures inside the module that I have to clean up later. – Rick Oct 21 '14 at 15:22
  • What I want to do is simply run python.exe and use my class there, easily, the same as I would `Input: x = 1` and `Input: x` then `Output: 1`. But it's beginning to seem like that isn't a simple thing to do. – Rick Oct 21 '14 at 15:25

1 Answers1

5

This might be what you're attempting. Note that I also use Anaconda.

My path:

C:\Users\...\Documents\Python Scripts\

import_sample.py

class class_sample(object):

    def __init__(self):
        self.x = ["I", "am", "doing", "something", "with", "Python"]

test.py

from import_sample import class_sample

c = class_sample()
y = c.x
print " ".join(y)

Result:

I am doing something with Python
[Finished in 0.1s]

Notice how being in the same folder allows me to import without having to install, per se. Basically, just make sure that the modules you need are in the same folder as your main.py and you're good.

EDIT:

Done from the terminal.

enter image description here

Notice how I cd into the above folder and activated python there. Since I was inside that folder, any modules inside it can be imported without any problems, alongside other system-wide modules installed as well.

WGS
  • 13,969
  • 4
  • 48
  • 51
  • Is there a way to do this from the python command window and skip the test.py step? I want to just be able to quickly create an object and test a method I just wrote by typing the lines `import mymodule`, `myobject = mymodule.myclass()`, `myobject.mymethod()` and then getting some output in the same window. – Rick Oct 21 '14 at 15:29
  • 2
    Go into your folder containing the module, type `python`, and `import` from there. Don't run `python.exe`, that's defeating the purpose of Python being on the path. You're perfectly allowed to do it from virtually any folder. I'll be editing my answer with an example. – WGS Oct 21 '14 at 15:34
  • Ahhhhhhhhhhhhhhhhh. I see! I knew it would be a simple answer. Thanks! – Rick Oct 21 '14 at 15:39
  • 1
    Glad to help. I love Anaconda to bits and pieces and it's always nice to help out someone using the distro. Enjoy! – WGS Oct 21 '14 at 15:40
  • 1
    I just realized that you spend an awful lot of time with Excel, similar to myself. If that is so, I sincerely recommend the `pandas` library. Since you're using Anaconda, it's supposed to be installed right away. It's far and wide much better to use Pandas than VBA. – WGS Oct 21 '14 at 15:42
  • Thanks again. I've already discovered xlwings but I'll give pandas a look as well. – Rick Oct 21 '14 at 15:44
  • 1
    It's also worth looking at the IPython notebook (also comes with Anaconda). – asmeurer Oct 22 '14 at 17:09
  • I use anaconda too on a win7 system. Can you give me a tip about how to access common modules which I develop myself? I'd like to store the common modules on a dropbox folder, which will be in a separate folder to the other python modules that call them. I have tried placing a path reference to the common module folder in the PATH environmental variable, but that doesn't seem to work. – dreme Mar 15 '15 at 09:14
  • @dreme See [this answer](http://stackoverflow.com/a/4002933/2437514). Read about the [PYTHONSTARTUP](https://docs.python.org/3/using/cmdline.html#envvar-PYTHONSTARTUP) variable and point it to a file that imports the modules you want when Python starts. – Rick Dec 01 '15 at 22:46