在哪里保存python模块 [英] Where to save python modules

查看:125
本文介绍了在哪里保存python模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在学习python 3.5中的模块。虽然我通常可以使用sudo pip install {package}安装和导入软件包,但我似乎无法弄清楚如何导入我自己的文件。



我做了一个测试带有单一定义的.py文件进行测试。我将它保存到site-packages文件夹中。我似乎无法从那里导入。我需要帮助了解如何导入文件。



我在线阅读有关可能使用sys.path但是,我不知道它是如何工作的。

解决方案

如果我有以下文件结构:

  / home / foo 
/home/foo/__init__.py
/home/foo/bar.py

我希望

  import foo.bar 
foo.bar.somefn( )

我首先需要这样做:

  import sys 
sys.path.append(/ home / foo)

注意 init .py文件,它告诉python查找子模块。在这种情况下我们不一定需要它,但最好有:



__init__.py适用于什么?



然而,由于这可能会重复,令人生畏并难以追踪,有很多工具可以帮助您进行设置,因为您的代码扩展为具有依赖关系和大量文件/文件夹。我建议你阅读一下pip / disttools / easy-install以及如何使用'setup.py'创建一个包。



什么是setup.py?



此外,您可能还想要探索虚拟环境和部署解决方案,如Buildout和Anaconda。这些有助于保持代码分离,因为您有多个项目可以满足不同需求,甚至不同的python构建和平台要求。当您将代码部署到其他计算机时,您可以更轻松地复制项目的需求。



以下是一系列文章,其中包含解释从packages,pip和setup.py到使用Buildout启动工作环境的所有内容的信息。看起来像一个伟大的系列:



http://reinout.vanrees.org/weblog/tags/softwarereleasesseries.html



制作setup.py:

https://docs.python。 org / 3 / distutils / setupscript.html


I'm just learning about modules in python 3.5. While I can usually install and import packages using sudo pip install {package}, I can't seem to figure out how to import my own files.

I made a test.py file with a single definition to test. I saved it to the site-packages folder. I can't seem to import from there. I need help understanding how to import files.

I read online about possibly using sys.path however, I don't know how that works.

解决方案

If I had the following file structure:

/home/foo
/home/foo/__init__.py
/home/foo/bar.py

and I wanted to

import foo.bar
foo.bar.somefn()

I would first need to do:

import sys
sys.path.append("/home/foo")

Notice the init.py file, which tells python to look for submodules. We don't necessarily need it in this instance, but it's good practice to have:

What is __init__.py for?

However, since this can get repetitive, daunting and tough to track, there are lots of tools available to help you setup as your code expands to have dependencies and lots of files/folders. I suggest you read a bit about pip/disttools/easy-install and how to make a package with a 'setup.py'.

What is setup.py?

In addition, you might want to explore the world of Virtual Environments and deployment solutions such as Buildout and Anaconda. These help keep the code separated as you have several projects on the go with different needs, or even different python builds and platform requirements. When it comes time to deploy your code to other machines, you'll have a much easier time replicating the needs of the project.

Below is a series of articles with info explaining everything from packages, pip and setup.py to starting up your work environment with Buildout. Seems like a great series:

http://reinout.vanrees.org/weblog/tags/softwarereleasesseries.html

The official docs for making a setup.py:

https://docs.python.org/3/distutils/setupscript.html

这篇关于在哪里保存python模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆