Module instead of package #399
-
I have just created a new project by using
Now I would like to create sdist. After executing On another machine (centos 7) I have installed this sdist:
Now when I try to use pyscaffoldmodule on this machine (centos 7) it looks like this:
What should I do in order to instead of In pycharm's "Python console" I can use my scaffmodule like this:
If I use pycharm's "Make directory as Sources Root" and set it to the project's
Finally, if I pycharm's "Make directory as Sources Root" and set it to the project's
Is there a way to set pyscaffold in a way to be able to use my module as the last example above shows? Versions and main components
|
Beta Was this translation helpful? Give feedback.
Replies: 9 comments
-
Hello @ghuname, welcome to PyScaffold's issues page, nice to see that you are using it 😄 So what is happening is related on how Python's module system and When you install a wheel/sdist package that uses the There is a trick however: for Python's import system I don't use PyCharm, but I suppose the only reason you are able to do In case you are interested, there is a bit more in-depth information about Python's module system in the following references: Please let us know if this solves your problem. |
Beta Was this translation helpful? Give feedback.
-
Hi @abravalheri
Why shouldn't I. PyScaffold is a quite good tool. I really helps in packaging the python code.
Do you want to say that if I delete
scaffold.py is jusst an example. My real code has the following structure:
Would it be OK, if I move all the code from
Locally I installed scaffmodule package as:
Before opening this issue, I tried to read everything I could about python packaging system. |
Beta Was this translation helpful? Give feedback.
-
I have put
After installing the sdist package on another computer, I am able to use (I don't know should I say module or package) it as I want:
This approach is OK, if using scaffmodule as a library, but what if I want to use it as script - or to make things more complicated - what if I want to use it as standalone shell script, something similar to For example, let's say that scaffmodule knows how to send an email. In python code I will import the module, send mail and exit. |
Beta Was this translation helpful? Give feedback.
-
Hello @ghuname, I will try to reply to your questions bellow:
This approach will probably work, but I never tested myself. The default configurations generated by PyScaffold are though for a project with more than one file, so it might be the case some minor adjusts are necessary. But in theory I don't see anything preventing it from work.
There are 2 ways of running scripts from modules/packages installed via pip/PyPI. The first one is to use the The second option is to use a [options.entry_points]
# Add here console scripts like:
# console_scripts =
# script_name = package.module:function If you uncomment these lines and change them according to your package structure, when your package is installed, The best way of supporting libraries and executable scripts at the same time in a single Python project is to first define your library API so people importing your project can use it, then create a wrapper function around it that uses For example, if you have a look on the skeleton.py PyScaffold have created for you, you can think about the API being the Then we have a CLI wrapper around it, defined in the run and main functions. When we register Finally, since we also add
If you always work in different machines using the I hope this helps... |
Beta Was this translation helpful? Give feedback.
-
Hi @abravalheri, I have just created a brand new scaffmodule package. I have created a new src/scaffmodule/demo.py file with contents:
After making sdist and installing it in new conda environment, I am able to use it with no further actions as you explained before:
So, for packages (that contain modules) everything works out of the box beautifully. Now I added to setup.cfg > [options.entry_points] the following:
After reinstalling the package and executing:
We can see that we are not collecting sys.argv arguments because we are calling print_arguments() function directly. According to skeleton.py I modified demo.py in the following way:
and changed setup.cfg to:
It works:
At this point I can confirm that when PyScaffold is used for a package that contains module(s), everything works out of the box smoothly, which is remarkable. Now my dilemma from the very beginning. @abravalheri Can you please tell me, what should I do now with scaffmodule in order to simplify imports? I can provide zip with scaffmodule if it will help. |
Beta Was this translation helpful? Give feedback.
-
@ghuname to avoid imports like
|
Beta Was this translation helpful? Give feedback.
-
@FlorianWilhelm I have just put all the code from demo.py to the init.py, and in setup.cfg changed entry_points to:
After that, I am able to:
Everything works as expected. |
Beta Was this translation helpful? Give feedback.
-
Hi @ghuname, I also did a quick test here and replacing |
Beta Was this translation helpful? Give feedback.
-
Just an aside, if your module is a single file Similarly, project directories don't have to be named the same as the module. Consider this directory structure as an example
(I hope my indentation survives so the above is readable.) And then when I package it up and upload it to pypi I name the package "esters_folly". Enjoy the choices you have with Python. :-) |
Beta Was this translation helpful? Give feedback.
@ghuname to avoid imports like
import scaffmodule.scaffmodule
you really have only two options as far as I know:scaffmodule.__init__.py
.scaffmodule.scaffmodule
and have inscaffmodule.__init__.py
: