1

I'm in the middle of transferring my work away from PyCharm and into VS Code to minimize the number of IDE's open at the same time. As I've been doing that, I've noticed that importing is markedly different:

My layout is as follows:

Projects
- PythonProjects
-- ThisProject
---- TheScriptIRunPath
----- MyScript.py
---- MyModuleToImport.py
-- OtherProject
- NotPythonProjects

In PyCharm, I would just enter from PythonProjects.ThisProject.MyModuleToImport import * and the import would work without a hitch. However, none of the imports seem to work this way in VS Code. I started on pylint and tried all of the suggestions here, none of which did the trick.

My original PyCharm organization just used the root environment for everything, so I didn't really pay any attention to venvs or build __init__ files.

However, one step I've been taking is separating my Anaconda environments to limit their size for each project. While picking the corresponding environments is straightforward enough in VS Code, I'm going to have to import modules from different environments, and I suspect that this is going to complicate importing modules from distinct environments even further.

But, first things first, what's the most hassle-free way to get my (hopefully PyCharm-styled) relative imports to work in VS Code in the same environment? Also, what else do I need to keep in mind as I start importing modules from separate environments?

I'm using Python 3.8.

Joshua Harwood
  • 337
  • 1
  • 2
  • 15
  • I'm surprised that you'd want to move away from PyCharm. I use VSC as well, but PyCharm is night and day better than anything else out there for working with Python code. No worries though. Whatever works. Best of luck to you! – CryptoFool Oct 23 '20 at 21:10
  • PyCharm eventually got so big that it was causing the programs I ran in it to halt due to a lack of physical memory. Also, all of my frontend work is done in JavaScript/ReactNative now, so I would have to keep two IDE's open and jump between them. WebStorm didn't look like a solution, so off to VS Code I went. – Joshua Harwood Oct 23 '20 at 21:15
  • Ah. Makes perfect sense. – CryptoFool Oct 23 '20 at 21:17
  • Importing from a different environment doesn't seem like a good idea to me. Either use one environment or install your multiple libraries in your different environments. – darcamo Oct 23 '20 at 21:23
  • 1
    Making your package "installable" and installing in your environment as necessary is a much cleaner solution IMHO (no hacking with `sys.path` and relative imports will work). [Here](https://stackoverflow.com/questions/64107888/importing-module-from-a-directory-above-current-working-directory/64110410#64110410) I have answered a question where I describe the process using [poetry](https://python-poetry.org/). For conda environments you can create [setup.py](https://packaging.python.org/tutorials/packaging-projects/) and use it to install your package in your conda environment. – darcamo Oct 23 '20 at 21:25
  • I followed the first of the two recommendations. Many thanks! – Joshua Harwood Oct 24 '20 at 00:04
  • @Steve, I got it working, and then I ran back to using PyCharm for instances when I don't need access to both (turns out one of my imported modules' functions was stalling the mentioned program, and the data issue disappeared after a factory reset). – Joshua Harwood Oct 24 '20 at 01:49

0 Answers0