Online Marketing

How to Setup a new Python Project

My name is Stephanie. I am member of the local part user group and please welcome Felix wick. He will talk about how to set up a new point project thanks and welcome to my talk. So, as we already heard so, this will be about how to set up a new pie’s in project, so this will be quite of an introductory talk, but there might also be a few interesting tweaks for maybe more advanced PI’s and users.

So it might also be interesting for them. So what to do when you start a new pie’s in project, so the first thing you you need to do is you want to start implementing, but before you can start implementing, you have to set up all that stuff. So you you just have all your ideas together, you know which packages you want to use and everything. But first of all you have to do the boring stuff before you can really start coding and what you usually need.

First, is you need an environment? So you might have on Python interpreter installed on your system and the side packages on your system, but you usually have several projects and not just one. So you need some kind of tool to to manage two different projects and the different dependencies which you might have, and you just need a let’s say: project, specific environment, for your for your different projects – and there are a virtual NFS – is a really cool tool to Do that and you can just use pip to install all your stuff there.

So, first of all the few words about virtual end. So, as I said, this is just a tool to create an isolated Pisan environment and what I mean with isolated is that you get an own Pisan interpreter for each of your projects and you get an own side packages for each of your projects. So the only thing you need to have to install it is you you just yeah. You just need to have virtual end on your on your global.

Let’s say global pious, an interpreter package, so it just install it via pip or other sources, and then you can create a new virtual environment by just typing in virtual end and then the name of the virtual environment. It’s just as easy as that, and this then directly comes with we’ve set up tools and pip installed in the you new in to your new virtual environment, and you can then there’s a small helper script, activate which you can use to just at your new virtual End to your to your path variable, so that means you can when you point it type pipe install of something else.

Afterwards, it just gets installed to your virtual environment, for example, and to get out of it you just type deactivate, and then you can directly jump to another virtual environment if you work on different projects at the same time. So this is really a very good tool for that. So then a few words about tip. So you probably all of you note, but so this is just in a tool to install and manage all your packages and it offers installation from the package index.

If it’s a local or global or sauce or binary distribution, so whatever you have so, for example, to get to get an install the latest version of a package. In this case, I just chose the package PI scaffold, which we will learn about in a few minutes from the public, PI API or trust type in pip, install PI scaffold. So just you have done very often, I think so, but it gets more interesting if you have something like versions of your packet switcher, which you add, which are different in your different projects, for example.

So then you can use the requirement specify us in pip install so, for example, here you just take only versions with triad and 0.7 or equal 0.7. So, and what’s even nicer is that you can also use just a whole requirement file to to get all your all your dependencies in once, so it just use this minus our option and you do all your your requirements. You just type them in in your package. In your file requirements txt for example, so if you’re then afterwards type in Patrice, you see what you have installed.

So in this case it’s a just PI scaffold and a few other things. You will see it later in a trot installation. So, okay, so now you have your your environment, so you can really start with your project. So what what you need to do to organize it? So the first thing is, you need, of course, a version control system, so this you need all the time and for this talk and for the demonstration later, I trust use git, because I think this is a good choice.

But there are a lot of other good tools also so and after you have this this setup. So this is another thing which you have to do. Then you need the reasonable directory structure and you need, of course, tests and documentation. So these are also things which you have to take care that have a good folder structure for it and that you have to the right tools and so on and so on. So what is a good directory structure for it? So, first of all, there are a few words about what I mean in the in the following with module and package.

So module is nothing ends, then a file containing Python code, so something which, with the dot PI ending, for example, and a package. This is a this is a folder which contains an inner PI file and all of your other PI’s and modules which you want to have in this package. So, in this case, you have to you have to package my package, which is in a bro check, which has the same name. So this is usually a good practice that you just name your project after your package or vice versa, and in this my package, you have this unit PI file.

So it is just the thus in principle that you can import all the stuff with the inside of your directory structure and you have your your modules in which do the actual stuff. So your actual implementation in module 1 and module 2 in this case, and you might also have some pack the sub packages in if you are doing different things and your package and then in each of your sub picture. Sub-Packages there’s another in a PI file electric and import all that stuff afterwards properly.

So this is the first step so and then you need to test the tests and the documentation, so this usually should go to separate folders so which I just call here. Tests for the unit tests and Docs for, for example, is Fink’s documentation and inside your test tests folder. You should have another init PI file and you should have all your for each of your modules. You should have a separate test module which just starts with the with test underscore and then the module name.

So then you have it really in ordered way, and you know what belongs to what and you can start afterwards. The test with, for example, setup tools or whatever, so this will, you will see in a moment so in a docs folder, you have all your restructure text text files, for example. So this might be this index rst, which is the more or less index.Html of things, and you might have a configuration PI and a make file.

If you have those things set up beforehand and there’s also another file which is called set up, PI about which I will also talk in a few minutes so or actually, even though, so, how is your the next step? If you have then implemented everything have everything in order and what you need to do, then, is you want to to tell the stuff to other people, so you want to distribute it in some way, so um how to do that? So there are pison comes with an onboard tool which is called these tutors.

So this this is actually it is basically the set up pi, which i, which I showed you so, but there’s also something are called set up, tools which you heard and which is cat, which comes with literal end, so which is more or less just an extension Of this of dis duty, so usually I would just recommend to you setup tools. Then you have all this stuff inside and there might also be effort might also off distribute.

So this is a another thing which is something in between so this was kind of a fork of setup tools, but it’s more or less an older version. So um just you set up tools or these tutors. If you don’t want the additional features. Okay, then, if you have your package, everything in order – and you want to install it now in your virtual length – so then you just type in pious and setup. I install and this installs every everything which you have just implemented into your virtual environment and what is helpful during development is this setup I develop command because then there are just links created in your site packages in in your virtual environment, and this means that if You change something in your source code implementation.

Then it directly changed the installation, individual environment, otherwise you would have to type in pip, install pious and setup. I install again afterwards, you have changed something. Okay, and if you want to pack it and ship it, then you can just use the setup. Is this tour set up by beedis commands, so this means the source, distribution or a binary distribution so per default. This will be a zipped tar ball and for the binary distribution is of course dependent on the machine.

So this we will also see later in the demonstration okay. So now we have done all this stuff. So but if you want to ship something to somebody, then you have to give it a version, because afterwards you have will then have a new version a few weeks later, a few months later, and then one was maybe 1.0 and some at some time. You have a 1.0 or something so you need to update that version stuff all the time.

So that means you need to update a version attribute of your package or module, and you need to to get the self identification in the inner PI file, and you need to update an argument in this in insider PI itself that so a meta information of the Setup PI that you just have the right name for your package and also have to the right thing and in for the meta information for the pi PI server, for example – and this is really cumbersome to do this manually because then, if you really have to do This all the time you will forget it, and then you will have to do it again, so you need something which is just as this automatically for you and what is a cool small tool.

Is this version your package, so this this manages just the versions by kit. So this means that you can just do a good tech if you want to release so you’re, just the get tagged version or point zero and afterwards you directly have the right version number and if you then do an STI be destroyed. I wrecked Lee have the the version of your last get text, so this is really cool. What makes it really simple? Okay, so this would be the let’s say: bass basic behavior of of the the set up play and if you then want to do your tests and your documentation also, we’ve set up high, so dagger that comes with whether for unified unit package just comes on board.

So just type eyes and set up by test, and it runs all your tests in your test folder. If you have set up your set up PI or correctly, it just runs them all through with unit test. So maybe you want to use Pais the PI test. Instead, because unittest is okay, but maybe you want to do coverage or something else, so then there’s the possibility to have in in setup by this command class argument. So there you can justify node command classes, let’s say where you can use them PI test.

Instead of unit test, for example, and just run it with setup, I test or you want to do something like PI flakes or whatever, so you cost the contrast. The do everything you want within set up a set up high, and what is a quite good thing to do, for example, is to run the documentation by it. So you override your command, this command class thing, and then you can, for example, to set up by docks and you just run your documentation with we’ve set up by itself.

Otherwise, you would have to type in all the type make item a HTML or something like that. Okay, so now I have talked a lot about setup. Pi, so you’ll see a bit of this boring code now so um. In this case, I just imported from setup tools: the setup function, which is basically what is in there and a small helping function, fine packages and what is really the the basic of this of this setup function is the name and the version.

So this is also what your package will be called afterwards, so your top boil, for example, if you do an S test, and you need to tell them why our packages, what has to be included in the distribution so in there, you can, for example, use this. Fine packages where you include everything but just exclude the test folders, for example – and this install requires this is this – is the testing in beforehand before you install your package.

If all this stuff is already there, and if it’s not there, it would install it afterwards. Why are so easy installed, but, as we have already done it beforehand with with our requirements file, we can also at this position, just use the requirements file, for example, and we’ll have it also again in one in one place. Yes, you will also see later so the tests hood. This is just a test which need to be run by the by our setup by Testament or command, as we have already seen, and what is the nice functionality? Is this entry points, so there are in this.

In our package, my package, which we had before there was this module one and inside this module one there might be a function run and this function run gets maybe some arguments and you can trust if you, if you set it up this way, you can trust Afterwards, in the under command line and type run, and then the options of this, and then you will just run this function through. So this is an interesting thing if you, for example, want to start a web server or whatever trust.

Why? I your why I this entry point, okay, so to to set up a Pisan project you you need to do quite a few things, so you need to think about a good project structure. You have to set up a git repository or something or another version control system. You have to to do all the tweaks with your setup PI’s. So there are a lot of a lot more commands than the ones I’ve shown you. You have to take care of the versioning and everything.

So all this stuff is is pretty: let’s say, tough, if you, if you are a new person user or at least boring, if you, if you are experienced, so you don’t want to do this all the time again so there. This was the case also for us when we, when we did this over and over again for small project. This was why we just implemented a small tool which is called buy a pie, scaffold which just does this for you.

So you can just install it by a pie, pie, so pip install pie, scaffold and the sources are available on github so, and it only requires virtual enfin and get to be installed on your system and setting up a pro checklist and just as simple as put Up my bro check afterwards and then you will have everything so now I just give you small or short demonstration. So let’s say we: we want to have a virtual environment first, so already done so now we have a virtual environment in our directory.

So we saw that we have to to activate it. So now it’s activated, it changed the prompt which are which are also saw on the slides. So now, what do we want to do? We want to just install PI scaffold and if the network works, this will do it. Ok done so check what we have inside our virtual environment. So the PI scaffold is there in the version 2.7 at the moment. So now we need a new project. So just put it up already done so then we have our project folder, my project.

Inside there we have now, for example, this set up pi. We have the the project folder my project. We have our test folder and our documentation folder. So, for example, we look into tests there’s only the unit, PI n, also in my project, so there’s there’s another thing, this version and version your PI. This comes from version here. So don’t have to go into details about that. So you don’t need to care about it.

Usually so, and then we have this requirements file, for example, so that you could now put all your requirements. For example, there would be an umpire or sci-fi moment, and this manifest in is also something which, which you might know this is about Estes. So this is a kind of a template file where you can just include what you want to include in your source distribution, and what is good practice is to have something like a readme here so which you could also use them in your as meta information.

In your setup PI, for example, to have this directly on anti ti and now, let’s see what we wanted to do, we wanted to implement all it so already done so our version was was unknown for the moment, so we might techno version so which might be Version 0.1 and we call it first or something so then, if we look to to get K, then you conc almost nothing but there’s now a new, a new tech in there, and so now we could, for example, do Pisan set up PI s test so and Now we have a folder dist in here, and here we have now our our our topper.

So this my project, oh that one target set. So this is now the the version which is gets from version year. Yr this this kid tech and the name which we gave in in our setup hi. So let’s have a look in the in the setup file shortly so that you see what’s actually happening there. So you need you. We didn’t need to do anything of this by our own. It just took our fill order, meta information which we have to use this command class and also has the the this Docs and setup Docs and testing already configured so yeah.

Basically, what else we wanted to do so we wanted to have maybe something like and beta stall, so that you see this this too. So this is then, an in this tanaga, another tar ball, which is dependent on the on your machine and if we want to run now, for example, the test, so this is as easy as that. So, as we has no test also, the coverage is perfect, so and if you want to now to run, for example, this Daxing, so there we see this will fail because we have no another installed things up to now.

This is the only thing which you, which would then need to do, oops this way so done, and this will then just build your documentation takes a moment. So then we have everything in our Doc’s file, for example. So if we now want to to have a look, how this work oops, this will look like so just close this and look into you. So we have our our project and have to the stocks folder, and then it comes into the HTML and there you have than just your index.

Html, and you see your your documentation, for example, which we which we just wrote. So this is now more or less the module reference and what you are, what you could have in your injury txt so and it’s basically it so yeah as as you want to know more, so you can now, of course, ask now. I just come to our to our boosts afterwards and then we can discuss everything thanks a lot for the talk. Are there any questions? Yeah, please come forward to the microphone, so everybody can hear things what about the wheel packaging, yet a wheel packaging? You could just do with we’ve set up higher beta steel, for example, and then you have directly the two wheel there.

So usually wheel is a good thing to do so we can just just do it. So this you read, do something like that, so you need to, of course, install wheel beforehand. No, okay, not a good choice! This question: that’s why I’m asking it’s always missing, like it’s, the new cool format and it’s somehow a lot of doctors so know you. Can you can do it? It’s sorry, ah good thing to do yeah, mmm. Of course we need to do that first and then we can do it and then we also have it as wheel in our in our dist.

Thank you. Are there any more questions? Okay thanks a lot


By Jimmy Dagger

Find out my interests on my awesome blog!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.