0

Im having some issues with virtual enviroments, i have a pycharm project where i want to launch it from my terminal, however when launching the project it seems like my internal modules are not found. How do i let python know about the internal importing of modules in my project. And also shouldnt the virtual env take care of the dependencies created along my python files?

this is what i write in the terminal:

(venv) C:\Users\####\PycharmProjects\CLONE_1\Initialize>python CLONE_MainInitialize.py
Traceback (most recent call last):
  File "C:\Users\####\PycharmProjects\CLONE_1\Initialize\CLONE_MainInitialize.py", line 6, in <module>
    import Variables as v
ModuleNotFoundError: No module named 'Variables'

(venv) C:\Users\mknjbh\PycharmProjects\CLONE_1\Initialize>

when i run the project it cannot find my module Variables, which is a directory in my python project, the directory has a __init__.py which imports the files in that directory so that i can use the files, and all works fine in the pycharm IDLE.

I used the venv from the python.exe interpreter.

So what can i do about this. Let me know if you need more information or code.

Any ideas as how i can solve this would be great i am always eager to learn more.

  • write a `setup.py` and install your package in development mode..? – thebjorn May 05 '22 at 11:53
  • @thebjorn But if the packages is local files does that work the same? – Mathias Albrechtsen May 05 '22 at 12:02
  • @bad_coder Hey thanks for the suggestion, however it doesnt solve my problem, i dont have a problem with the imports from within pycharm, however running a cmd promt and running a file from my project there will cause the project to not find the imports. I could add the PYTHONPATHS however since this project CLONE_1 is a clone of a MAIN project, then the PYTHONPATHS would lead to the MAIN project, and not the CLONE. also the whole thing should happen autonomously So i need a way for the CLONE project, to identify the different local import with some command or file that enables that. – Mathias Albrechtsen May 05 '22 at 13:32
  • @bad_coder i have tried to use the setup.py, but it still is not regonizeing the modules, from the python CLONE_MainInitialze.py command. Also i suppose i could make some script that updates the PYTHONSPATHs if i did set them up in PYCHARM, however it just seems wierd that there isnt a better way of handling this kind of operation. – Mathias Albrechtsen May 05 '22 at 13:35
  • @MathiasAlbrechtsen no problem, it's possible you've been hit by a recent regression in an old bug [see PY-23156](https://youtrack.jetbrains.com/issue/PY-23156) and also the older [PY-976](https://youtrack.jetbrains.com/issue/PY-976). This is also happening to me after I updated to PyCharm 2022.1, it's a very frustrating bug. If that is the case don't worry even if everything is correct with `setup.py` the modules will show as not found anyway. The workaround for now is just marking the base directory of the project as sources root and wait for the next PyCharm version release. – bad_coder May 05 '22 at 14:31
  • @MathiasAlbrechtsen launching from the terminal itself you have to remember to **activate the venv**, see how [in this page](https://docs.python.org/3/tutorial/venv.html#creating-virtual-environments) – bad_coder May 05 '22 at 14:33
  • @bad_coder Thx alot, yeah it is very wierd. it still has problems even though i mark base directory as root. and running the file from the activated venv. I do have the PyCharm 2022.1 so i guess it might be that problem, lets hope it gets fixed soon :) – Mathias Albrechtsen May 05 '22 at 14:48
  • @bad_coder so what works for me is if i add sys.path.append("PATH") however isnt there some better way of going about this since its quite alot of paths that needs to be adressed? and also ive heard that it isnt the most viable solution using that sys.path command – Mathias Albrechtsen May 05 '22 at 14:53
  • @MathiasAlbrechtsen from the error message you're using virtualenv? See [this post](https://stackoverflow.com/q/41573587) there's a lot of different virtual environment managers and the rules between them may differ slightly. Your question lacks a lot of the step-by-step diagnostic info that is necessary, consider using the `where` command [see this post](https://stackoverflow.com/a/6847060) and if you want edit the question to identify explicitly what kind of venv you are using and where everything is exactly. We also don't have your `setup.py`, etc... Narrow your search to your venv. – bad_coder May 05 '22 at 15:00
  • activate the venv on the terminal!!! – bad_coder May 05 '22 at 15:02
  • @bad_coder Thanks, i will take that into account for other questions or redefining of the current one, your help is very insightfull and is much appreciated. It has helped me alot, Thanks:) – Mathias Albrechtsen May 05 '22 at 15:06

0 Answers0