In order to create a Python package, it is very easy. The __init__.py file is necessary because with this file, Python will know that this directory is a Python package directory other than an ordinary directory (or folder – whatever you want to call it). Python is an outstanding language for people learning to program, and perfect for anyone wanting to 'get stuff done' and not spend heaps of time on boilerplate code. Arcade is a Python library for creating 2D video games that is easy to start using, and very capable as you gain experience.
Active7 years, 1 month ago
I want to create a 'library' of Python modules which I will be able to access from several separate project folders.
For example, I want the Python scripts in /proj1/ and /proj2/ to have access to /lib/.
I don't want a single directory with all the Python scripts, as this seems rather disorganized. I also definitely don't want to copy the same /lib/ script into each of the different projects.
What is the ideal way to handle this in Python? Is it appending to Python's path? Or is this more of a hack? This seems to have the disadvantage of making the files less portable. Or is it this question/answer about using relative paths? Or something else?
I should add that I'm interested in Python 2.x rather than 3.x, if it matters.
Community♦
David CDavid C4,73333 gold badges4141 silver badges6060 bronze badges
3 Answers
Turn
Ignacio Vazquez-AbramsIgnacio Vazquez-Abramslib/
into a package, then put it in one of the directories in sys.path
(or add a new entry). You can then import e.g. lib.help
into your projects.614k111111 gold badges11081108 silver badges11961196 bronze badges
Follow the standard road that everyone takes: make your code a proper Python package with a proper setup.py. The benefits are: easy_install'able, easy distributable, easy generation of command line script (through console_scripts entry point) etc....
Andreas JungAndreas Jung
I think the best tool you can use to keep under control the environment of your project is virtualenv. You create a new virtual environment and install there your packages, then you run your project using the python executable that virtualenv provides you.
Probably you should use distutils in your library, in this way installing it with virtualenv is really easy, because virtualenv installs setuptools or distribute in the virtual environment, thus allowing you to install in the virtual environment packages from pypi or from your local machine.
There is also the possibility to create a custom bootstrap script that automatically installs some libraries after the virtual environment creation.
BakuriuBakuriu70.7k1212 gold badges149149 silver badges180180 bronze badges
Not the answer you're looking for? Browse other questions tagged pythonpython-2.x or ask your own question.
About
troposphere - library to create AWS CloudFormation descriptions
The troposphere library allows for easier creation of the AWS CloudFormationJSON by writing Python code to describe the AWS resources. troposphere alsoincludes some basic support for OpenStack resources via Heat.
To facilitate catching CloudFormation or JSON errors early the library hasproperty and type checking built into the classes.
Installation
troposphere can be installed using the pip distribution system for Python byissuing:
To install troposphere with awacs(recommended soft dependency):
![Create Create](http://i.imgur.com/oAZVd0I.jpg)
Alternatively, you can run use setup.py to install by cloning this repositoryand issuing:
Examples
A simple example to create an instance would look like this:
A simple example to create an instance (YAML) would look like this:
Alternatively, parameters can be used instead of properties:
And
add_resource()
returns the object to make it easy to use with Ref()
:Examples of the error checking (full tracebacks removed for clarity):
Incorrect property being set on AWS resource:
Incorrect type for AWS resource property:
Missing required property for the AWS resource:
Currently supported AWS resource types
- AWS::AmazonMQ
- AWS::Amplify
- AWS::ApiGateway
- AWS::ApiGatewayV2
- AWS::AppMesh
- AWS::AppStream
- AWS::AppSync
- AWS::ApplicationAutoScaling
- AWS::Athena
- AWS::AutoScaling
- AWS::AutoScalingPlans
- AWS::Batch
- AWS::Budgets
- AWS::CertificateManager
- AWS::Cloud9
- AWS::CloudFormation
- AWS::CloudFront
- AWS::CloudTrail
- AWS::CloudWatch
- AWS::CodeBuild
- AWS::CodeCommit
- AWS::CodeDeploy
- AWS::CodePipeline
- AWS::CodeStar
- AWS::Cognito
- AWS::Config
- AWS::DAX
- AWS::DLM
- AWS::DMS
- AWS::DataPipeline
- AWS::DirectoryService
- AWS::DocDB
- AWS::DynamoDB
- AWS::EC2
- AWS::ECR
- AWS::ECS
- AWS::EFS
- AWS::EKS
- AWS::EMR
- AWS::ElastiCache
- AWS::ElasticBeanstalk
- AWS::ElasticLoadBalancing
- AWS::ElasticLoadBalancingV2
- AWS::Elasticsearch
- AWS::Events
- AWS::FSx
- AWS::Glue
- AWS::Greengrass
- AWS::GuardDuty
- AWS::IAM
- AWS::Inspector
- AWS::IoT
- AWS::IoT1Click
- AWS::IoTAnalytics
- AWS::IoTEvents
- AWS::KMS
- AWS::Kinesis
- AWS::KinesisAnalytics
- AWS::KinesisAnalyticsV2
- AWS::KinesisFirehose
- AWS::LakeFormation
- AWS::Lambda
- AWS::Logs
- AWS::ManagedBlockchain
- AWS::MediaLive
- AWS::MediaStore
- AWS::MSK
- AWS::Neptune
- AWS::OpsWorks
- AWS::OpsWorksCM
- AWS::PinpointEmail
- AWS::RAM
- AWS::RDS
- AWS::Redshift
- AWS::RoboMaker
- AWS::Route53
- AWS::Route53Resolver
- AWS::S3
- AWS::SDB
- AWS::SES
- AWS::SNS
- AWS::SQS
- AWS::SSM
- AWS::SageMaker
- AWS::SecretsManager
- AWS::SecurityHub
- AWS::Serverless
- AWS::ServiceCatalog
- AWS::ServiceDiscovery
- AWS::StepFunctions
- AWS::Transfer
- AWS::WAF
- AWS::WAFRegional
- AWS::WorkSpaces
Currently supported OpenStack resource types
- OS::Neutron::Firewall
- OS::Neutron::FirewallPolicy
- OS::Neutron::FirewallRule
- OS::Neutron::FloatingIP
- OS::Neutron::FloatingIPAssociation
- OS::Neutron::HealthMonitor
- OS::Neutron::Pool
- OS::Neutron::LoadBalancer
- OS::Neutron::Net
- OS::Neutron::PoolMember
- OS::Neutron::Port
- OS::Neutron::SecurityGroup
- OS::Nova::FloatingIP
- OS::Nova::FloatingIPAssociation
- OS::Nova::KeyPair
- OS::Nova::Server
Todo:
- Add additional validity checks
Duplicating a single instance sample would look like this
Community
We have a Google Group, cloudtools-dev, where you can ask questions andengage with the troposphere community. Issues and pull requests are alwayswelcome!
Licensing
troposphere is licensed under the BSD 2-Clause license.See LICENSE for the troposphere full license text.