-1

OK, prerequisites:

  • It's my first ever Python project. I used to do some scripting but never anything bigger
  • So I'm at the very beginning of a learning curve. It's like when you can't kill an ant in Fallout 2 Temple level. On later levels, I was really good in Fall 2:)

Problem:

I can't figure out how to import a module written by me and placed in a different folder

Context:

  • The project I'm intended to create is meant to do a lot of measures conversions. So I decided to store in DB all data in the same unit system & keep all conversions upon user preferences on a codebase level

  • In a different folder I decided to store tests. To write the very first one (testing the abovementioned module) I need to import the module, but here is the story begins. I know it's classic, but I'm completely messed with import

Toolkit:

  • PyCharm Pro (PyCharm 2021.3.1)
  • Python 3.7 interpreter
  • macOS 10.15, Intell

Set up: Settings screenshot provided

  • Project structure. Folders are marked as Source & Test
  • I need to import from conversions.py to test_conversions.py
  • PYTHONPATH settings like this

What do I, for the sake of God, need: with all the abovementioned, how do I import conversions.py to test_conversions.py or any other place of my project? I read a number of articles and it's getting me anywhere (contradictory, 2.x related, etc). I feel like I need a piece of more foundational info but as well I need a clear walkthrough, a code snippet to import bloody file, I really appreciate any kind of advice

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • Is `conversion` intended to be a package, or just a folder containing top-level modules? – Brian61354270 Jan 28 '22 at 00:38
  • @Brian I don't expect any other files to be added there -- just a single file with all formulas I need and a precision I'm OK with. As far as I understood -- a package in Python is a bunch of files, but I don't need it, a module is OK for me – Viktor Shemchuk Jan 28 '22 at 00:42
  • Welcome back to Stack Overflow. As a refresher, please read [ask] and note that this is *not a discussion forum*. We are not interested in your background as a programmer, or in your frustrations with the project. We *are* interested in a clearly asked question, with as much information as possible in text directly in the question itself. (Use an ascii representation for folder structures, such as you might get from the `tree` command-line program.) – Karl Knechtel Feb 01 '22 at 23:18
  • Anyway, the simple and ordinary way to make `conversions.py` available to `test_conversions.py` is to *install your project in the venv*; then an ordinary absolute import should work - just as it works for standard library modules. Being able to do this without impacting your system Python is *what the venv is for*. – Karl Knechtel Feb 01 '22 at 23:21

3 Answers3

1

imports are a bit tricky. The issue you have is where your python is looking for packages. I would discourage you to add to your PYTHONPATH a specific project but you could do that locally in your file.

A much easier way is just to launch your test file from the top directory. In this case your import will just be import conversion.conversion

And then you can launch your test from the root folder with python -m tests.conversion.

In Pycharm you can use the interface to deal with that, see this link. But I like the python -m because it works from anywhere, not only inside Pycharm.

Ssayan
  • 938
  • 5
  • 12
0

make a class inside a conversion.py, then you can import it from test_conversion.py.

conversion.py

class convert():
   def km_to_mm(input):
        output = input * 1000000
        return output

then import it in test_conversion.py

input = 0.001 # specify your input value
from conversion import convert
converted = convert().a_to_b(input)

converted will have value 1000 make sure you use the same folder. otherwise should use folder name to import. like

import foldername.conversion
from foldername.conversion import convert
dimz
  • 179
  • 8
0

I really appreciate all of you who tried to help. I got the problem solved in a very ridiculous manner, below is to someone who might face the same issue in the future.

So, in case you see the next:

  • You use PyCharm (no idea how other IDEs behave)
  • You created a module & want to import it into other files of your project
  • You type import module_name
  • While you type it, the string looks active and autocomplete even proposes you your module name but as only you finished typing, the import string turns grey, PyCharm throws you a warning saying Unused import statement, yellow bulb next to the import string suggests you delete the import string

--> This does not mean you are not able to import your module, it means you've done it and now can call anything from your module in the code below.

This taught me to pay some more time to read docs before jumping to using anything new and think better about UX in anything I do.