Python 如何使用 Anaconda 从 Windows 中的任何目录运行项目文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26488981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:32:49  来源:igfitidea点击:

How to run project files using Anaconda from any directory in Windows

pythonanaconda

提问by Rick supports Monica

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

Python新手在这里。根据朋友的推荐,我正在使用 Anaconda(在 Windows 7 上)。

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.

我希望能够做的是在 Notepad++ 中对我的类进行更改,然后立即在我的 Python 命令提示符窗口中对其进行测试。

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>)?

这是一个我似乎无法找到答案的简单问题:Anaconda 的默认安装希望我将 .py 文件存储在哪个目录中(以便我可以轻松地使用 加载它们import <MODULE NAME>)?

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

我的 PATH 变量设置为: C:\USERS\<USERNAME>\Anaconda3;C:\USERS\<USERNAME>\Anaconda3\Scripts

(this is the default)

(这是默认值)

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

我应该在Scripts目录之外工作吗...?里面已经有很多文件了。

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.

大多数人做什么?也许将另一个文件夹添加到 PATH 变量并在那里工作?您是为每个项目向 PATH 添加一个新文件夹还是有更有意义的方法?我已经有一个用于其他所有内容的 Projects 目录。我只想能够在那里工作。

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).

我在 Notepad++ 中编码。我真的不想费心设置/学习 IDE(我只是在做相对简单的 I/O 文件操作,我以前一直在 Excel 中进行...恐怖)。

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.

问题是我从“开始”菜单运行 python.exe。我没有意识到您应该在您正在工作的文件夹(SHIFT+RIGHT CLICK)中打开一个 CMD 窗口(例如C:\USERS\<USERNAME>\MY PYTHON STUFF)并从那里运行 python。

采纳答案by Jerome Montino

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

这可能就是你正在尝试的。请注意,我也使用 Anaconda。

My path:

我自己的路:

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

import_sample.py

导入样本.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.pyand you're good.

请注意,在同一个文件夹中如何让我无需安装就可以导入,本身。基本上,只需确保您需要的模块main.py与您的文件夹在同一文件夹中,并且您很好。

EDIT:

编辑:

Done from the terminal.

从终端完成。

enter image description here

enter image description here

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

注意我cd是如何进入上述文件夹并python在那里激活的。由于我在该文件夹中,因此可以毫无问题地导入其中的任何模块,以及安装的其他系统范围的模块。